viyancs
viyancs

Reputation: 2339

How to execute javascript code after appendChild

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.

Upvotes: 1

Views: 6049

Answers (2)

Michael Sandino
Michael Sandino

Reputation: 1958

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

Upvotes: 4

Selvakumar Ponnusamy
Selvakumar Ponnusamy

Reputation: 5563

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

Upvotes: 1

Related Questions