...

View Full Version : How can I load a slow external JS after page loads?



nicky
07-01-2011, 01:27 AM
I have a slow loading external JavaScript that displays a calendar widget. When I put it in my sidebar, while the page loads, it cuts off where the calendar should be until the JavaScript for it finishes.

How can I make the script load after the page finishes loading?

I tried creating a function at the end of the HTML and calling the function in the sidebar, but I think because the function was being called before the script was executed, it didn't work.

I appreciate any and all help!

BulletTimeBill
07-01-2011, 09:55 AM
document.onload = CalenderWidget;

Should load it once all the html has loaded, maybe not images and such. Not sure, give it a go though. Although i'd be more concerned that something as simple as a calendar widget is taking so long to load.

devnull69
07-01-2011, 10:25 AM
It's window.onload and not document.onload

BulletTimeBill
07-01-2011, 11:15 AM
I meant what I said. Waiting till the document is loaded, not the window. You can apply .onload to many things. window, document, and image are the most useful (or most used).

BulletTimeBill
07-01-2011, 11:19 AM
Actually no, try window. Apparently it does have issues I never looked into.

nicky
07-01-2011, 01:57 PM
In the sidebar I put



window.onload = function() { document.write('<script type="text/javascript" src="http://www.somewebsite.com/calendar.js"></script>'); };


And when I refreshed the page, it showed the design for a fair second, then went to a all white screen that had nothing by the calendar.

devnull69
07-01-2011, 03:51 PM
Just forget about document.write. It is only(!) useful at the time the page is being created. Once the document is ready (or window.onload) document.write will create a new document by deleting the old one completely.

Please use .innerHTML on existing elements or document.createElement() to create new ones "on the fly".

Logic Ali
07-01-2011, 04:30 PM
Provided that script does not use document.write:
window.onload = function()
{
var se = document.createElement( 'script' );

se.type = 'text/javascript';

se.src = "http://www.somewebsite.com/calendar.js";

document.getElementsByTagName( 'body' )[ 0 ].appendChild( se );
};

nicky
07-06-2011, 02:38 AM
This did not work :(


Provided that script does not use document.write:
window.onload = function()
{
var se = document.createElement( 'script' );

se.type = 'text/javascript';

se.src = "http://www.somewebsite.com/calendar.js";

document.getElementsByTagName( 'body' )[ 0 ].appendChild( se );
};

Logic Ali
07-06-2011, 03:15 AM
This did not work :(The code works in a test case, but as I warned there could be a document.write or something else in the third-party script that precludes it being loaded following page load.
As I dont know what the script is or even if it's being used properly, it's not possible to comment further.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum