P1n*_*u1n 0 javascript innerhtml undefined
我尝试使用JavaScript获取脚本标记的innerHtml.
var scripts = document.getElementsByTagName("script");
console.log(scripts.length);
console.log(scripts[0].innerHtml);
Run Code Online (Sandbox Code Playgroud)
这个脚本日志2和undefined,但为什么当有发现2个脚本它返回undefined?我需要获取脚本的innerHtml.Anny帮助将不胜感激:)
编辑:
更改innerHtml为innerHTML它后仍然在控制台中引发错误:
Uncaught TypeError: Cannot read property 'innerHTML' of undefined (line 34)
Run Code Online (Sandbox Code Playgroud)
第34行是这样的:
var scripts = document.getElementsByTagName("script"); //line 32
for (var i = 0, imax = scripts.length; i < imax; i++) { //line 33
var scriptstring = scripts[i].innerHTML; } // line 34
Run Code Online (Sandbox Code Playgroud)
顺便说一下,脚本不是嵌入的.js文件,它是在.html文件中硬编码的
编辑2:整个.html文件
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Test</title>
<script type="text/javascript">
onmousemove = function test1() {
window.alert('hello');
}
</script>
<script>
window.onload = function() {
var scripts = document.getElementsByTagName("script");
for (var i = 0, imax = scripts.length; i < imax; i++) {
var scriptstring = scripts[i].innerHTML;
scriptstring = scriptstring.replace(/(\r\n|\n|\r)/gm,""); //convert the innerHTML to one line, easier to match
if (scriptstring.match(/.*(function) (test1).*/) != null) {
scripts[i].parentNode.removeChild(scripts[i]);
}
}
}
</script>
</head>
<body>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
innerHtml
Run Code Online (Sandbox Code Playgroud)
应该:
innerHTML
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3892 次 |
| 最近记录: |