Link to home
Start Free TrialLog in
Avatar of wallaceds77
wallaceds77

asked on

Javasript and Iframe Interaction on a PHP Page

I have a weird problem that has presented itself. I have some Javascript loading on a PHP through a Function/Load into a div layer. On the pages that have an Iframe, the Javascript usually loads without a problem. On the pages that don't have an Iframe, it doesn't load at all. I have trouble-shot it down to the Iframe and that's where the trail goes cold. Any ideas?
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

No, you need to give a link at least so we can check it out.  Basically, iframes and javascript have nothing to do with each other unless you have programmed some interaction.
Please tell us the browser and give us a link to your demonstration example, thanks.
Avatar of wallaceds77
wallaceds77

ASKER

Hi, sorry for the lack of example information. My test site is located at:  

http://severeweathercenter.com/rwd/

You'll notice that the About, Safety Tips and Disaster Kit links are somehow not playing well. The Javascript clocks at the top of the page aren't loading properly. Those same clocks are loading on the Iframe pages including the Index/Home, Tornado Warnings, Radar, and all the Mesoscale Analysis pages. AS far as I can tell, everything is identical with the exception of the Iframe.
'clocks.php' loads fine all by itself.  But 'clocks.js' is only found on the main page, not on the 'About' page.  You do have a duplicate id="wrapper" which can cause problems with javascript which expects 'id's to be unique.  And you have header content like '<meta...' in the middle of the page where it doesn't usually go.

And you should probably get a more current version of jQuery.  http://jquery.com/download/
One of the helpful tools is the W3 Validator.  It can tell us if we have errors in the markup.  JavaScript will sometimes work even when there are errors in the markup, but it's a crap-shoot.
http://validator.w3.org/check?uri=http%3A%2F%2Fsevereweathercenter.com%2Frwd%2F&charset=%28detect+automatically%29&doctype=Inline&group=0
Thank you for your review, Dave.  Each site page (Index, About, Radar and such) is split up into pieces. There is a 'header.php' and 'footer.php' that contain code relevant for each page (all the meta tags, css, js, nav). The PHP files are being called together into each individual user page through a PHP Includes function. I had the 'clocks.js' being called-in to the 'clocks.php' page, but have since added it to the 'header.php' called-in on each page. I don't necessarily see the '<meta...' you mention. Is it in the 'clocks.php' page? I changed the 'wrapper' id, thanks.

As for the jQuery. I'm using some 3rd party scripts/code. At one time, I looked at updating the jQuery, but things started to break, so I decided to leave it with the revisions that were supported.

I guess I figured there was something I was missing, like an 'Onload' or some trigger that an Iframe was emulating.
In order to see what is actually in the page, I use a Firefox Add-on called Web Developer.  It has a function to view the 'Generated Source' that you get after the javascript is run.  The 'meta' elements appear to be coming from 'clocks.php' because it is generating a complete stand-alone page.
Now you need to make 'clocks.js' load After 'clocks.php' is loaded.  When 'clocks.js' runs first, it has errors because the 'div's that it is looking for do not exist yet.
I placed the 'clocks.js' after the 'time' div in the body but no resolution. I appreciate the good tips, I'll show this as answered. I know that an Iframe on the page will trigger the clocks to load properly.
ASKER CERTIFIED SOLUTION
Avatar of Alexandre Simões
Alexandre Simões
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you, Alexandre Simões! That was absolutely perfect! Javascript is not my strength, but this helped!