admin管理员组文章数量:1333628
For example:
<html>
<div id="media">123</div>
<a href="javascript:void(0)" onClick="return fun()">Click</a>
<script type="text/javascript">
function fun() {
document.getElementById("media").innerHTML = '<script type="text/javascript">alert("working");<\/script>';
}
</script>
</html>
After you click the alert does not show.
alert("working")
; is just an example. I want it to finish one job other javascript
. I have a job to be processed through ajax should have used innerHTML
For example:
<html>
<div id="media">123</div>
<a href="javascript:void(0)" onClick="return fun()">Click</a>
<script type="text/javascript">
function fun() {
document.getElementById("media").innerHTML = '<script type="text/javascript">alert("working");<\/script>';
}
</script>
</html>
After you click the alert does not show.
alert("working")
; is just an example. I want it to finish one job other javascript
. I have a job to be processed through ajax should have used innerHTML
- Possible dupplicate of stackoverflow./questions/1068517/…. – antoyo Commented Mar 14, 2013 at 18:56
3 Answers
Reset to default 7The HTML spec specifies that script tags inserted using innerHTML should not be executed. This is a security consideration.
There are still ways to do this if you are determined, such as adding it to img
handlers or creating a script element, inserting it into the DOM and changing its text
property. I will not elaborate on these, since doing this is generally considered somewhat sketchy. If you are not trying to inject script, you should include the script element in the page source.
<script type="text/javascript">
function fun() {
alert("working");
}
</script>
<div id="media">123</div> <a href="javascript:void(0)" onClick="fun();">Click</a>
Your JS function should be like this.
function fun() {
alert("working");
}
本文标签: javascript in innerhtml not workingStack Overflow
版权声明:本文标题:javascript in innerhtml not working - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742342307a2456846.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论