views:

214

answers:

3

I'm working on a WordPress site that has two external javascript files load about half-way down the page. The files are badges from Reddit and Digg, and often add about 4-8 seconds to the total loading time of page — while also preventing the bottom 50% of the page from loading too.

The Digg and Reddit javascripts render an <iframe> (which I assume needs to load completely before the rest of my web page is loaded), and thus adds a big amount of extra load time.

I tried moving the <script> tags to the bottom of the page, right before </body>, but the badges render just below the footer instead of where they need to be.

How can I force these two external javascript files to load last, but still render where they need to?

A: 

Where you render the script tags shouldn't necessarily imply where the UI those scripts generate appears-- that it does seems a goofy or questionably designed. Do the scripts have known functions in them that let you hand them a container div or something in which to render their UI?

You're correct, of course, in loading them last, before </body>. That's the right strategy.

quixoto
+2  A: 

You could create a div ["divA"] where you want your Digg/Reddit iframes to go [So this is basically just a placeholder]. When the page is done loading, append a "script" element to head so the Digg/Reddit scripts can load. When they're done loading, you can move them from body to divA.

document.body.removeChild(iframe);
document.body.getElementById("divA").appendChild(iframe);
ItzWarty
This will cause the `src` of the iframe to load twice.
Renesis
+1 Having the snippets `document.write` their stuff at the document, and moving it to the right location using the DOM sounds like the smartest solution. Maybe not remove the element, though, but have it all in one DIV that gets *moved* to the right place?
Pekka
Renesis you brought up a valid point. Thanks. How would you "move" the element? I guess you could use javascript's offsetTop, offsetLeft , and parentNode to find the x/y position of the "temporary" div and stick your iframe over it.
ItzWarty
@ItzWartzy - Unfortunately there's probably not a *great* solution. This was the subject of the entire discussion here: http://stackoverflow.com/questions/2233097/how-can-i-stop-an-iframe-reloading-when-i-change-its-position-in-the-dom
Renesis
A: 

The quick answer is - no simple way. If you user defer="true" on the script tags, and they use document.write, it will still write out the content where it was when it loaded and not where the script tag was inserted.

Some of the answers in this question might help you: http://stackoverflow.com/questions/2233097/how-can-i-stop-an-iframe-reloading-when-i-change-its-position-in-the-dom

Some of the other answers in this thread suggest removing the iframe and then reinserting it. However, that will lead you straight to the problem in the above-linked question -- it will cause the content of the iframe to load twice.

The general approach will be to put a placeholder where you want them and then move them into that placeholder when they are ready.

Renesis