2

I know my subject is quite tricky but i dont know how to much more ellaborate it on the subject alone. so here how it goes. i have a button

<a href="#" onClick="loadtheFile()">Load IT!</a>

on the script tag:

function loadTheFile() {
var script = $("<script><\/script>");
script.attr("type", "text/javascript");
script.attr('src','http://www.thisismyexternalloadingjsfile"');
$('body').append(script);
alert("done! the file has been loaded");
}

the script well, when loaded will automatically have a modal box. but the problem is, my alert seems to fire first than what is one the script

so how will i know if i have finished to load the script?

update for the first attempt to answer:

function loadTheFile() {
    var script = $("<script><\/script>");
    script.attr("type", "text/javascript");
    script.attr('src','http://www.thisismyexternalloadingjsfile"');
    $('body').append(script);
    $(document).ready(function() {
      alert("done! the file has been loaded")};
    }

same problem

Joseph Bada
  • 222
  • 2
  • 13
  • Do you have control over remote script or not? – skobaljic Nov 16 '15 at 09:58
  • What remote script are you trying to load? – Gabriel Sadaka Nov 16 '15 at 09:59
  • there is a typo onclick="loadtheFile" and function name is loadTheFile – brk Nov 16 '15 at 10:05
  • @gabriel the script file was given to me so that i could attach it on our website, once the script file is loaded it pop a modal box which will have user interactions and sends data on a CRM-like structure, so what im doing is to present that script. handled by an event. – Joseph Bada Nov 16 '15 at 10:15

4 Answers4

6

alert does indeed run before the script has been loaded. All that appending the script tag to the page does is append the script tag to the page. Then the browser has to download the script and, once received, run it. That will be after your loadTheFile function has exited.

So you need to get a callback when the script has actually be loaded and run. This is more standard than it used to be, but still has some cross-browser hassles. Fortunately for you, jQuery's already solved this problem for you (since you're using jQuery already):

function loadTheFile() {
    $.getScript('http://www.thisismyexternalloadingjsfile"')
        .then(function() {
            alert("done! the file has been loaded");
        });
}

Re your comment:

but my script file has data-* attributes

Assuming you're talking about data-* attributes on the script tag, then you'll have to do a bit more work, but it's still fairly straightfoward:

function loadTheFile() {
    var load = $.Deferred();
    var script = document.createElement('script');
    script.src = 'http://www.thisismyexternalloadingjsfile"';
    // No need for `type`, JavaScript is the default
    script.setAttribute("data-foo", "bar");
    script.onreadystatechange = function() {
        if (script.readyState === "loaded") {
            load.resolve();
        }
    };
    script.onload = function() {
        load.resolve();
    };
    load.then(function() {
        alert("done! the file has been loaded");
    });
    document.body.appendChild(script); ;// Or wherever you want to put it
}

The onreadystatechange bit is to handle older versions of IE.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0

Rather than forge the script with text and jQuery, just use native Javascript:

var s = document.createElement('script');
s.onload = scriptLoaded;
s.src = '/path/to/my.js';
document.body.appendChild(s);

function scriptLoaded() {
    console.log('Script is loaded');
}
cloakedninjas
  • 4,007
  • 2
  • 31
  • 45
0

Try something along these lines:

Your main page:

function whenScriptIsReady(){
  alert('This function is called when the other script is loaded in!')
}
function loadTheFile() {
  var script = $("<script><\/script>");
  script.attr("type", "text/javascript");
  script.attr('src','myotherjs.js');
  $('body').append(script);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="#" onClick="loadtheFile()">Load IT!</a>

myotherjs.js:

alert('This will automatically run when the JS is loaded in!');
whenScriptIsReady();
Oliver
  • 1,576
  • 1
  • 17
  • 31
  • the other js is an external file of which i dont have access to write on. the other js will accepts, and send data. what im doing is to present that – Joseph Bada Nov 16 '15 at 10:08
0

JavaScript is executed asynchronously, so you alert will be executed before the browser can load the new script. If you want to execute logic after the script has been loaded, you could add an event listener to your script that will call the function 'loadFunc` once the script load is completed:

var loadFunc = function() {
  alert("External Javascript File has been loaded");
};

//Other browsers trigger this one
if (script.addEventListener)
  script.addEventListener('load', loadFunc, false);
Alex
  • 21,273
  • 10
  • 61
  • 73