最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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

发布评论

评论列表(0)

  1. 暂无评论