views:

598

answers:

2

In FireFox I have this jQuery at the end of the body:

$(document).ready(function() {
     $.getScript('LiveMapsJavascriptProvider.aspx?type=reference&value=6', init);
});

There are a lot of js files in the head that are needed to be all loaded before this would work. So I put my call in a document.ready event. It doesn't work. IE works fine.

If I put an alert(''); before I call $.getScript it works.

It looks like a problem with the scripts not getting loaded yet?

I thought Document.ready was fired after all the scripts are loaded and ready to go.

Thanks, ian

+3  A: 

document.ready is fired after the DOM is loaded. You may try this:

$(window).load(function() {
    // will execute once all scripts and images are finished loading
});
Darin Dimitrov
Think you meant `.load`.
Crescent Fresh
@Crescent, yes I meant `load`. Thanks for the remark.
Darin Dimitrov
+1  A: 

You don't necessarily need to use jQuery for that.

Simply have an onload function as below:

<body onload="JavascriptFunctionName">

Or you can dynamically attach your function call to the onload event as shown below:

function addEvent(obj, evType, fn){ 
 if (obj.addEventListener){ 
   obj.addEventListener(evType, fn, false); 
   return true; 
 } else if (obj.attachEvent){ 
   var r = obj.attachEvent("on"+evType, fn); 
   return r; 
 } else { 
   return false; 
 } 
}
addEvent(window, 'load', JavascriptFunctionName);

You may embed jQuery functions calls inside the JavascriptFunctionName function.

EDIT:

jQuery is also capable of doing that through the following code. I recommend trying that first, for the sake of avoiding unnecessary redundant code.

$(window).load(function() {
    JavascriptFunctionName();
});
Wadih M.
Because that's what I could provide to help OP. If you find any bug in the implementation of the addEvent function, please let me know. Upvotes/Downvotes should relate to how helpful the answer was, and I believe it was. You are free to go ahead and post a better answer.
Wadih M.
OP is evidently using jQuery already for `$.getScript`. Why in cripes would you recommend rolling another half-baked `addEvent` implementation when jQuery has this all wrapped up with `$(window).load(init)`? Binding the event is just the tip of the x-browser-event-ugly-iceberg: http://stackoverflow.com/questions/1796141/properly-bind-javascript-events/1796729#1796729
Crescent Fresh
It's not half-baked, it works perfectly and this is where it gains its value. Nonetheless, I understand the notion of unnecessary redundant code, so I've amended my answer. Thanks.
Wadih M.