1

I'm having a problem when I use appendChild() to append a <script> tag where the javascript referenced in the new script tag is not run. In Firebug I get a notification which says "Reload the page to get source" but if I reload the JS will be appendChild again.

Here is my code:

var divAd = document.createElement("div");
divAd.innerHTML = '<script src="http:example.com/adenseload.js" language="javascript" type="text/javascript"></script>';
titleBak.appendChild(divAd);

How can I use the appended JS after using appendChild()?

Thanks for your answer.

2
  • are you sure that the src-property is correct and the loaded js-file actually calls a method after being loaded? Commented Nov 17, 2011 at 10:59
  • yes the src is corrent.i'm appendChild() when load document in another js. Commented Nov 17, 2011 at 11:04

2 Answers 2

4

Don't wrap it in a div, create the script tag directly. This worked for me:

var scriptTag = document.createElement("script");
scriptTag.src = "http://example.com/myscript.js";
bodyTag.appendChild(scriptTag);
Sign up to request clarification or add additional context in comments.

3 Comments

i think you code is true but divAd.innerHTML = ajaxResult in my case is result from ajax so i cant create element script because already in result.
You could deliver only the url via ajax instead of the whole script-tag.
you true, why i'm not hink like that...stupid... i'm using code thi for get src from javascript tag var adScript = ad.getElementsByTagName("script"); var index = adScript.length - 1; var adsScr = adScript[index]; var src = adsSrc.src;
1

You have to use below statement to load JS,

document.write('<script src="', 'http:example.com/adenseload.js', '" type="text/JavaScript"><\/script>');.

you can append HTMLobjects only to the DIV, Not script tags

Comments

Your Answer

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.