动态插入< script>标签意味着工​​作? [英] Are dynamically inserted <script> tags meant to work?

查看:105
本文介绍了动态插入< script>标签意味着工​​作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码,其中< script type =text / javascript> 块被动态地插入。



该块包含一个函数,该函数的上面有一个< input type =button> 元素(也是动态插入的),用 onclick 属性。



然而,它不起作用,并且Firebug说我试图点击时没有定义函数按钮。



这是否可以预期,如果有的话,是否有解决方法?

div>

如果您将它写入innerHTML属性,它不会工作,除了在IE中DEFER属性出现时:

  scriptParentNode.innerHTML ='< span /> ;< script defer =defertype =text / javascript> blah();< / script>'; 

您的选择是,a)使用DOM创建元素并追加它:

  var head = document.getElementsByTagName(head)[0]; 
var sTag = document.createElement(script);
sTag.type =text / javascript;
sTag.text =blah();;
head.appendChild(sTag);

或b)在解析HTML时使用document.write(但不能在! / p>

 < head> 
< script type =text / javascript>
document.write('< script type =text / javascript> blah();< / script>');
< / script>
< / head>

编辑

<由于我对于defer属性的信息不正确,所以显然 我觉得有必要从 MSDN文档。 IE虽然从innerHTML字符串的开始处移除了NoScope元素,但可以通过在HTML字符串的开始处添加一个作用域元素(上面更新的示例)来解决此问题:

 < HTML> 
< SCRIPT>
函数insertScript(){
var sHTML =< input type = button onclick =+go2()+value ='Click Me'>< BR>;
var sScript =< SCRIPT DEFER>;
sScript = sScript +function go2(){alert('Hello from inserted script。')};
sScript = sScript +< / SCRIPT+>;
ScriptDiv.innerHTML = sHTML + sScript;
}
< / SCRIPT>
< BODY onload =insertScript();>
< DIV ID =ScriptDiv>< / DIV>
< / BODY>
< / HTML>

请随意在MSDN文档中单击显示我按钮以查看示例。 [链接]


I have some code where a <script type="text/javascript"> block is dynamically inserted.

This block contains a function, which has an <input type="button"> element above it (also dynamically inserted) call it with the onclick attribute.

However, it is not working, and Firebug says that the function is not defined when I attempt to click the button.

Is this expectable, and if so is there a workaround?

解决方案

If you're writing it to an innerHTML property, it won't work except for in IE when the DEFER attribute is present:

scriptParentNode.innerHTML = '<span/><script defer="defer" type="text/javascript">blah();</script>';

Your options are, a) use the DOM to create the element and append it:

var head = document.getElementsByTagName("head")[0];
var sTag = document.createElement("script");
sTag.type = "text/javascript";
sTag.text = "blah();";
head.appendChild(sTag);

or b) use document.write at the time your HTML is parsed (but not after!)

<head>
  <script type="text/javascript">
    document.write('<script type="text/javascript">blah();</script>');
  </script>
</head>

EDIT

Seeing as I was downvoted apparently for the information regarding the defer attribute being incorrect, I feel it necessary to post a working example from the MSDN documentation. Whilst it is true that IE removes NoScope elements from the beginning of an innerHTML string, it's possible to work around this by adding a scoped element to the beginning of the HTML string (example updated above):

<HTML>
<SCRIPT>
function insertScript(){
    var sHTML="<input type=button onclick=" + "go2()" + " value='Click Me'><BR>";
    var sScript="<SCRIPT DEFER>";
    sScript = sScript + "function go2(){ alert('Hello from inserted script.') }";
    sScript = sScript + "</SCRIPT" + ">";
    ScriptDiv.innerHTML = sHTML + sScript;
}    
</SCRIPT>
<BODY onload="insertScript();">
    <DIV ID="ScriptDiv"></DIV>
</BODY>
</HTML>

Feel free to actually click the "Show me" button in the MSDN documentation for a working example. [link]

这篇关于动态插入&lt; script&gt;标签意味着工​​作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆