javascript Document ready firefox (jQuery)

2019-02-20 05:30发布

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条回答
\"骚年 ilove
2楼-- · 2019-02-20 06:01

You can try using something like head.js to specify execution order, while still taking advantage of parallel loading.

查看更多
对你真心纯属浪费
3楼-- · 2019-02-20 06:09

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();
});
查看更多
我欲成王,谁敢阻挡
4楼-- · 2019-02-20 06:13

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
});
查看更多
登录 后发表回答