views:

51

answers:

3

Here's the odd situation:

we have a piece of javascript library that is being called on our onload of aspx page.

It works everytime for us, but the clients that have low speed modems get an error, because the object is not getting initialized and the aspx page is already loaded.!!

Is there any suggestions on how to call this piece of js code?

Thanks,

A: 

make sure you have your end tags.. i have seen onLoads in the not working right when your core tags are incomplete or not properly formatted

phil
A: 

With JQuery you can call your functions with ready event :

  $(document).ready(function() {
      // call your functions here
  });

The event will be called when the DOM is loaded.

Canavar
That may be true but the OP makes no indication as to the library he/she is using... It's almost pointless to throw code.
J-P
It's a recommendation, just like recommending to move his code to the end of the document. He doesn't mention where he calls his function, just 'onload'.
Canavar
+1  A: 

The onload even happens when everything in the page is loaded. If you have some script that is loading from a different server (ads, statistics), the onload event won't fire until those are loaded also. If their server is having problems, your onload may never fire at all, or after several minutes when the browser gives up waiting.

Instead of using onload you could put your code in a script tag as early as possible in the page, i.e. after the last element that the script needs.

If you have some external script that doesn't need a specific place in the page (statistics for example), you can move it to the bottom of the page to minimise the risk of interference with the rest of the page.

Guffa
It might be worth mentioning the Gecko `DOMContentLoaded` event and the multiple abstractions that have formed around it making it possible to use in non-Gecko browsers. This is normally a good compromise between the "onload" technique and the one you propose (individually placing script tags in places corresponding to the purpose of each)
J-P