0

I have abc.html page in which i am calling two javascript files as below in the head tag:-

<head>
  <script src="script_files/1.js"></script>
  <script src="script_files/2.js"></script>
</head>

In my 1.js file i am loading 3.js file using ajax call, as i don't need 3.js in all html files.

I am loading 3.js file by ajax call as shown below in the 1.js file:-

if(location.href.indexOf("abc.html") > -1 ){
    $.ajax({
        type: "GET",        
        url: 'script_files/3.js',
        dataType: "script",
        crossDomain: true,
        jsonp: false,
        error: function (XMLHttpRequest, textStatus, errorThrown) {
        },
        success: function () {
            console.log("Do Something");
        },
        async: false
    });
}

Now, when I am loading my abc.html in which i need 3.js file. its going to the 3.js ajax call but before it completes the ajax call it comming to the 2.js file method.

This problem is occurring in only Microsoft Edge browser.

Vinay
  • 705
  • 2
  • 7
  • 22

1 Answers1

0

I suggest to read this topic first:

Deep dive into the murky waters of script loading by Jake Archibald

EDIT: As connexo friendly pointed out, MS Edge does support onreadystatechange event since version 12, but it seems IE11 with content="ie=Edge" meta tag have to use onload event.

If your scripts are the web resources, you have no choice but to add onload event handler to each dependency. It may be the same function with a counter to know when they are all loaded. Then load all other scripts dynamically.

If you can access your scripts as local resources, you may load them the following way:

var sPath = 'Local_path\\script.js';
var oFSO = new ActiveXObject('Scripting.FileSystemObject');
var oFile = oFSO.OpenTextFile(sPath, 1, true, 0); // ForReading, ANSI
var oScript = document.createElement('script');
oScript.type = 'text/javascript';
oScript.text = oFile.ReadAll();
document.head.appendChild(oScript); // Sync loading
oFile.Close();

It reads the script as a text file, then loads its content as inline block, and thus even in MS Edge it is done immediately.