Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to dynamically load Javascript files and use them right away?
    primarykey
    data
    text
    <p>I am using JQuery to inject dynamically script tags in the body tab of a webpage. I got something like : </p> <pre><code>function addJS(url) { $("body").append('&lt;script type="text/javascript" src='+url+'&gt;&lt;/script&gt;'); } </code></pre> <p>I add several scripts this way, and try to use them right after. E.G :</p> <p><em>lib.js</em> </p> <pre><code>function core() {...} alert("I'am here !"); </code></pre> <p><em>init.js</em></p> <pre><code>addJS("lib.js"); c = new core(); </code></pre> <p><em>test.html</em></p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;title&gt;test&lt;/title&gt; &lt;script type="text/javascript" src="init.js"&gt;&lt;/script&gt; &lt;/head&gt; &lt;body&gt; Hello &lt;/body&gt; &lt;/html&gt; </code></pre> <p>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.</p> <p>I just don't get it o_O.</p> <p><strong>EDIT</strong></p> <p>I simplified this example, but Jeff answer made me understand that it was a mistake. So here are some details :</p> <p>init.js is not in the head of test.html when it reload because I inject it with a code exectuted on a bookmarklet.</p> <p>So the real execution process is the following :</p> <p>reload test.html > run the bookmarklet > jquery and init.js are inserted > lib.js is inserted</p> <p>Sorry for the confusion.</p> <p><strong>EDIT 2</strong></p> <p>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 ?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload