Google Chrome浏览器不接受.contentDocument或.contentWindow [英] Google Chrome Won't Accept .contentDocument or .contentWindow
问题描述
当我尝试我的代码时:
Testing.html-
<script language="JavaScript" type="text/javascript">
function find() {
var iframeEl = document.getElementById('love');
if ( iframeEl.contentDocument ) {
var form = iframeEl.contentDocument.document.getElementById('hi').getAttribute('href');
alert(form)
} else if ( iframeEl.contentWindow ) {
var form = iframeEl.contentWindow.document.getElementById('hi').getAttribute('href');
alert(form)
}
}
</script>
<body onload="find()">
<iframe name="lovez" src="frame.html" id="love"><a href="http://www.google.com" id="hi">Testingz</a></iframe>
</body>
Frame.html-
<a href="http://www.google.com" id="hi">Testing</a>
它不会返回警报框。但是在Internet Explorer上会。我一直在搜索互联网,尝试所有示例,但找不到一个可以在Google Chrome浏览器中运行的简单示例。我是在做错什么还是仅仅是Google Chrome?
It will not return an alert box. However on Internet Explorer it will. I have been searching the internet, trying all examples and can't find a simple example that will work in Google Chrome. Am I doing something wrong or is it just Google Chrome?
推荐答案
我注意到,两个如果代码来自Web服务器,则contentDocument
和 getSVGDocument
(用于svg文件中的svg对象)将正常工作,但是当我将代码复制到
I've noticed that both the contentDocument
and getSVGDocument
(for svg objects from svg files) will work fine if the code is from a web server, but when I copy the code to a local file, it will not work.
这是示例链接,其中包含我复制的代码,该代码可在网络上使用,但不能从磁盘使用。
Here's a sample link with a code I coppied from which works on the web but not from my disk.
这是解决方案我刚刚从Chrome浏览器中找到(感谢 Artem S ),即使用-allow-file-access-源文件
标志。
Here's the solution I just found from Chrome (thanks to Artem S), i.e. using the --allow-file-access-from-files
flag.
这篇关于Google Chrome浏览器不接受.contentDocument或.contentWindow的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!