html - How to execute javascript code after appendChild - Stack Overflow

I'm having a problem when I use appendChild() to append a <script> tag where the javascript

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/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.

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./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.

Share Improve this question edited Nov 17, 2011 at 11:08 Rory McCrossan 338k41 gold badges320 silver badges351 bronze badges asked Nov 17, 2011 at 10:56 viyancsviyancs 2,3394 gold badges40 silver badges72 bronze badges 2
  • are you sure that the src-property is correct and the loaded js-file actually calls a method after being loaded? – Michael Sandino Commented Nov 17, 2011 at 10:59
  • yes the src is corrent.i'm appendChild() when load document in another js. – viyancs Commented Nov 17, 2011 at 11:04
Add a ment  | 

2 Answers 2

Reset to default 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./myscript.js";
bodyTag.appendChild(scriptTag);

You have to use below statement to load JS,

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

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745079850a4610057.html

相关推荐

  • html - How to execute javascript code after appendChild - Stack Overflow

    I'm having a problem when I use appendChild() to append a <script> tag where the javascript

    17小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信