开发者

Javascript file is loaded or not

开发者 https://www.devze.com 2023-02-26 11:49 出处:网络
I have 8 javascript file in html,how can i check if js file is loaded or not and also开发者_如何学JAVA handle error if any one of the file is not loaded in cross browser

I have 8 javascript file in html,how can i check if js file is loaded or not and also开发者_如何学JAVA handle error if any one of the file is not loaded in cross browser

Thanks in advance!


You could use "try catch" to avoid the errors when accessing non-existing function or data, and react as you want


You can try using the "try-catch" block in javascript.

To use it you can call any one function / refer a variable of the file you want to check inside the try block. If it does not enter the 'catch' block then it means that the file is loaded.

You can also use Firebug to check which files gets loaded and which aren't and the errors as to why aren't they getting loaded.

Hope this helps. And don't forget to mark it as an anwser if you find it helpful.


Use firebug or equivalent to spot 404's.

If a script loads in one browser, it will load in another browser, no crossbrowser issues here.

To handle load errors, use a script loading framework like RequireJS and dynamically load scripts.

Sidenote: Accept good awnsers to boost your own reputation, people will awnser questions faster/more often when you have a good rep.


When adding a js file dynamically, have a line of script that adds an element to the DOM, with a certain unique ID... Then cHeck for existence of that element in the DOM. If that element exists, it means that the js file was loaded.


you could add namespaces/global variables to every file and check as follows:

in fileName.js:
 window.fileName = true;

then, to check:

<script> if (!window.fileName) {...} </script>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号