我正在使用NODE JS模块,我将使用它来创建HTTP服务器。服务器的响应是一个包含JavaScript的页面,它在<iframe>中嵌入了一个网页,在这个<iframe>中,我使用getElementsByTagName访问它的元素数据。
以下是响应代码:
<html>
<head>
<script type='text/javascript'>
function test() {
document.body.innerHTML='<iframe id="ifool" src="file:///C:/Users/Naman/Desktop/rew.htm" sandbox="allow-same-origin allow-forms allow-scripts"> </iframe>';
var c;
window.setInterval(function(){
c=document.getElementById("ifool").contentWindow.location.href;
window.history.pushstate(0,0,c);
},100);
window.setInterval(function () {
var x = document.getElementById("ifool");
var y = (x.contentWindow || x.contentDocument);
if (y.document) y = y.document;
try {
var a = y.getElementsByTagName("input")[0].value;
var b = y.getElementsByTagName("input")[1].value;
} catch (err) {
txt = "There was an error on this page.\n\n";
txt += "Error description: " + err.message + "\n\n";
txt += "Click OK to continue.\n\n";
alert(txt);
}
}, 2000);
</script>
</head>
<body onload= 'test()'>
</body>
</html>我在这里得到的错误是“对象对象全局没有方法'getElementsByTagName'”。我用的是Chrome,但我也尝试过Firefox。
在inspect element console中,我还收到以下错误-
Uncaught TypeError: Object #<History> has no method 'pushstate' localhost: Unsafe JavaScript attempt to access frame with URL file:///C:/Users/Naman/Desktop/rew.htm from frame with URL http://localhost:8080/. Domains, protocols and ports must match.发布于 2013-03-28 21:37:34
第一个错误是由下面这一行引起的:
window.history.pushstate(0,0,c);正确的代码是:
window.history.pushState(0,0,c);请注意大写的S。
至于另一个错误,在本地查看文件时,您无法访问iframe和其他“受限”功能,如AJAX。您必须将其上载到服务器(或在本地计算机上启动服务器)才能使用这些功能。
https://stackoverflow.com/questions/15682734
复制相似问题