Home > Web Front-end > JS Tutorial > body text

How to determine the success or failure of JS file loading in IE and IE6 browsers_javascript skills

WBOY
Release: 2016-05-16 16:13:39
Original
1261 people have browsed it

Browser file loading actually has very complicated compatibility issues. Recently I saw @lifesinger make a specific summary. The troublesome thing here is that IE6~8 does not distinguish between successful or failed loading, and all take a callback. A solution I saw online is to set a global variable at the end of the loaded file or change the attribute of the label to distinguish it, so that the success or failure can be judged by this flag. But it's obviously not perfect, and the file needs to be loaded.

Later I tried another idea, first create a vbscript, and set the src to a JS file. If the file loads normally, an error will definitely be reported, otherwise there will be no response. In this way, if window.onerror catches an error, it means that the file is valid and can be loaded normally. If not captured, a timeout will be triggered after n seconds.

This part of the code:

Copy code The code is as follows:

If(ie && ie < 9) {
        vbs = doc.createElement('script');
        vbs.language = 'vbscript';
         vbs.src = file;
        saveErrorHandle = win.onerror;
       win.onerror = function() {
          load();
          win.onerror = saveErrorHandle;
         return true;
};
setTimeout(function(){
​​​​​ ref.parentNode.insertBefore(vbs, ref);
}, 0);
} else {
       load();
}
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template