views:

658

answers:

5

I am using JQuery to inject dynamically script tags in the body tab of a webpage. I got something like :

function addJS(url) {
  $("body").append('<script type="text/javascript" src='+url+'></script>'); 
}

I add several scripts this way, and try to use them right after. E.G :

lib.js

function core() {...}
alert("I'am here !");

init.js

addJS("lib.js");
c = new core();

test.html

<html>
  <head>
    <title>test</title>        
    <script type="text/javascript" src="init.js"></script> 
  </head>
  <body>
     Hello
  </body>
</html>

Loading test.html pops up "I'm here" and then ends up with an error "core is not defined". Of course merging both of the JS files will make them work perfectly.

I just don't get it o_O.

EDIT

I simplified this example, but Jeff answer made me understand that it was a mistake. So here are some details :

init.js is not in the head of test.html when it reload because I inject it with a code exectuted on a bookmarklet.

So the real execution process is the following :

reload test.html > run the bookmarklet > jquery and init.js are inserted > lib.js is inserted

Sorry for the confusion.

EDIT 2

Now I have the solution to my problem (that was quick :-)) but I am still interested to the answer to my question. Why does this go wrong ?

+1  A: 

Notice your addJS function is appending to the end of the body element.

Since browsers will run scripts as they appear in the HTML source,

c = new core()

will run before your lib.js script is loaded (at the end of the body element).

I would recommend moving c = new core(); into the $(document).ready(function() {...}); or into a script element AFTER the body tag.

Jeff Meatball Yang
Ok, I am going to edit the question because you got a point but this does not appy here. I simplified my example but I realize it changes quite a lot of things. In fact the test.html already loaded because I inject init.js with a bookmarklet.
e-satis
+5  A: 

jQuery has this functionality built in with getScript.

Paolo Bergantino
Geez, it's going to be SO useful. Thks ! I don't clic on "solved" because I'd like to understand why my way to do it is wrong.
e-satis
+1  A: 

IMO, appending the script tag to the end of the document to load a script is rather unsightly. Reason:

  1. you are trusting the browser to automatically fetch the script and load it.
  2. You have no way of finding out whether the script is loading, has loaded, or if it encountered an error (maybe 404?)

The appropriate way would be to either use $.getScript(), or for a finer-grained control, fetch the script file with $.ajax() and use eval().

However, the second method has some issues: if you invoked eval() inside a function, then the script won't be available outside it! This mandates workarounds...

but why bother! use $.getScript() and get over with it :)

cheers, jrh.

Here Be Wolves
See the comment for Paolo Bergantino.
e-satis
But actually I have a way to no when the script is loaded with firebugf and console.log (and it is). Anyway the js files are in fact PHP files generating the js on the fly, and they have a logging system too so I know I'am not hitting a 404. But you are right to mention it.
e-satis
Yes, firebug can do that for you, but only at development time. Not at runtime.
Here Be Wolves
+1  A: 

In response to why your code is failing: Adding a script tag to the body does not block further script execution. Your code adds it, which starts the browser download process. Meanwhile, your script tries to call core(), which doesn't exist because lib.js hasn't finished downloading. jQuery works because it waits till the script finishes downloading before executing your callback function.

Karl Guertin
+1  A: 

You get the "core is not defined" error because the scripts are loaded asynchronous. Which means that your browser will start loading lib.js in the background, and continue executing init.js, and then encounter "new core()" before the lib.js has finished loading.

The getScript function has a callback that will be triggered after the script is finished loading:

$.getScript('lib.js', function() {
    var c = new core();
});
gregers