JavaScript Load Order

大城市里の小女人 提交于 2019-11-27 04:22:16

Is there a way to make sure both scripts load before I use them in my application.js?

JavaScript files should load sequentially and block so unless the scripts you are depending on are doing something unusual all you should need to do is load application.js after the other files.

Non-blocking JavaScript Downloads has some information about how scripts load (and discusses some techniques to subvert the blocking).

cross-domain scripts are loaded after scripts of site itself, this is why you get errors. interestingly, nobody knows this here.

in jquery you can use:

$(document).ready(function(){/*do stuff here*/});

which makes sure the javascript is loaded and the dom is ready before doing your stuff.

in prototype it looks like this might work

document.observe("dom:loaded", function() {/*do stuff here*/});

If I understand your problem correctly.. I think that may help..

If you don't want to rely on a lib to do this... I think this might work:

<script>
   function doIt() {/*do stuff here*/}
</script>
<body onLoad="doIt();"></body>

I had a similar problem to this, only with a single script. The solution I came up with was to use addEventListener("load",fn,false) to a script object created using document.createElement('script') Here is the final function which loads any standard JS file and lets you add a "post load" script.

function addJavaScript( js, onload ) {
   var head, ref;
   head = document.getElementsByTagName('head')[0];
   if (!head) { return; }
   script = document.createElement('script');
   script.type = 'text/javascript';
   script.src = js;
   script.addEventListener( "load", onload, false );
   head.appendChild(script);
}

I hope this may help someone in the future.

maxsilver

Is there a way to make sure both scripts load before I use them?

Yes.

Put the code you want loaded last (your application.js stuff) into prototype's document.observe. This should ensure that the code will load only after prototype + other stuff is finished and ready. (If you are familiar with jQuery, this function is similar to jQuery's $(document).ready )

AMQ depends on prototype which the error console in FireFox says object is not defined.

Do you mean that AMQ depends on the Prototype library? I can't see an import for that library in the code you've provided.

Do you mean that AMQ depends on the Prototype library? I can't see an import for that library in the code you've provided.

Yes for ActiveMQ's javascript (amq.js) does depend on Prototype. In the amq.js it loads 3 scripts, _amq.js, behaviour.js and prototype.js.

Thanks you for your help on the JavaScript load order wrumsby. This tells me that my bug is in another castle :(

I guess I have a different problem. I also checked the js files from ActiveMQ 5.0 to 5.1 and noticed they were the same as well. Something has changed in 5.0 to 5.1 that requires a refresh for the topics to subscribe. I'll keep looking, but thanks for eliminating this possible cause.

You can also use the built in SharePoint javascript method to control the execution of your scripts;

_spBodyOnLoadFunctionNames.push("yourFunction");
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!