11

I'm dynamically inserting a <script> tag with a src attribute and no content. But the browser does not pull down that src and run the script after the insertion -- the tag just sits there in the DOM.

Is it possible for me to tell the browser to "run" the script tag?

Because of the other code I'm working with, it's easier for me to keep the code fetched via the src attribute than to fetch it myself and insert it into the body of the tag -- but if that's necessary, I can do that too (and welcome any advice on that).

update with requested info

  1. The script tag is inserted based on user interaction an arbitrary number of times after the page has loaded
  2. I'm inserted the tag like this (jquery's html function strips out script tags): document.getElementById("my-div").innerHTML = "the script tag, which stack overflow wants to strip";
asked Feb 11, 2011 at 20:36
3
  • You could wrap the contents of the inserted .js document in a function and call it after it's inserted Commented Feb 11, 2011 at 20:40
  • are you using window.write to insert the script tag? Commented Feb 11, 2011 at 20:41
  • Try @ksiegel's advice and use a traffic monitor like Firebug or HTTP Watch to see if the script is retrieved. Commented Feb 11, 2011 at 20:44

3 Answers 3

4

Try following code:: Its working

 var script = document.createElement( 'script' );
 script.type = 'text/javascript'; 
 script.src =MY_URL; 
 $("#YOUR_ELEMNT_ID").append( script );
answered Feb 11, 2011 at 21:00
Sign up to request clarification or add additional context in comments.

4 Comments

Trying it now. For some reason append won't attach the script object to my div. i tried attaching some other arbitrary text and it did work. still experimenting...
getting close... seem to have better luck starting with var preview_script = $( 'script' );
i mean var preview_script = $('<script></scr' + 'ipt>');
got it working, via no jquery -- Manish, I edited your answer with the code that worked for me -- check it out and accept it if you agree.
2

it should run right after the browser inserts the script into the dom, but you can try to wrap your script into a function and call that function after the script loads :

var script = document.createElement('script');
script.setAttribute('type','text/javascript'); 
script.setAttribute('src',script_url); 
script.onreadystatechange= function () {
 if (this.readyState == 'complete') 
 initScript();//the whole script is wrapped in this function
}
script.onload= initScript;

Hope this helps!

answered Feb 11, 2011 at 22:21

Comments

0

You could have a setTimeout in the main script to check if the new script is there.. and if it is, then run it. Do you know if it downloads the file or if it just does nothing? Check with Chrome's Network monitor.

I think there's a dynamic way to load Javascript without the src= tag, as well, but I haven't personally done it.

Why not have all the javascript pre-loaded and just put through some inline JS to execute it?

answered Feb 11, 2011 at 20:39

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.