View Single Post
Old 10-21-2011, 09:08 PM   PM User | #7
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,210
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Blaze: But how do you ensure that the JavaScript that you loaded with your trick then actually gets run? Call a function in that code that you know must exist?

One problem with this is that the loading (assignment to the src) will be *synchronous*. So if there is any hangup in getting the response from the server, the HTML page will be "dead"...no other JS code can run, events, can't happen, etc.

That's why people prefer using AJAX. The first A there means "Asynchronous". So while the loading from the server is taking place, all other code on the page is accessible. The completion of the load triggers the onreadystatechange event. If the load hangs, the page keeps running.

A variation on your trick might be this:
Code:
<iframe style="display: none;" id="loadarea"></iframe>
<script type="text/javascript">
 window.setInterval("document.getElementById('loadarea').src = 'http://www.mysite.com/update.js?nocash=' + Math.random()*500;",60000);
</script>
*NOW* the load *IS* asynchronous, by definition. And the code in the iframe can automatically start running itself, rather than waiting for a call from the parent. And, of course, the code in the iframe can refer to anything in the parent via parent.someFunction() or similar.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote