Javascript脚本元素设置内部文本

VSP*_*VSP 13 html javascript iframe code-injection

我们需要在iframe中添加一个javascript元素(它在同一个web /域中,因此不会附加安全问题).我们得到了它的工作,但不知道如何填写其标签之间的脚本内容...你会怎么做?

var iframe = document.getElementById('iframeX');
var iframedocument = iframe.contentWindow.document;

var script = iframedocument.createElement('script');

script.setAttribute('type', 'text/javascript');
script.innerText = 'alert(\'hello\')'; //this doesnt work
script.value= 'alert(\'hello\')'; //this doesnt work either

var head = iframedocument.getElementsByTagName("head")[0];

head.appendChild(script);
Run Code Online (Sandbox Code Playgroud)

iframe文档中的所需结果:

<head>
    <script type="text/javascript" >
        alert('hello');
    </script>
</head>
Run Code Online (Sandbox Code Playgroud)

Pra*_*man 14

你试过了吗:

script.setAttribute('type', 'text/javascript');
script.innerHTML = 'alert(\'hello\')';
Run Code Online (Sandbox Code Playgroud)

  • 是的,这就是为什么我说你是对的(并且赞成你):innerText不是标准的,并且没有在FF(*Firefox*)中实现.对不起的缩写很不好(甚至不是Firefox的首字母缩略词). (3认同)
  • 好点:设置innerText不是标准的. (2认同)