admin管理员组

文章数量:1321605

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 ?

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 ?

Share Improve this question edited May 25, 2009 at 7:34 Bite code asked May 25, 2009 at 6:49 Bite codeBite code 598k117 gold badges309 silver badges335 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 5

jQuery has this functionality built in with getScript.

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();
});

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 remend moving c = new core(); into the $(document).ready(function() {...}); or into a script element AFTER the body tag.

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.

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.

本文标签: bookmarkletHow to dynamically load Javascript files and use them right awayStack Overflow