谷歌浏览器不会接受.contentDocument或.contentWindow! [英] Google Chrome Won't Accept .contentDocument or .contentWindow!

查看:951
本文介绍了谷歌浏览器不会接受.contentDocument或.contentWindow!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我试用我的代码时:
$ b Testing.html -

 < script language =JavaScripttype =text / javascript> 
函数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 =lovezsrc =frame.htmlid =love>< a href =http://www.google.comid =hi> Testingz< ; / A>< / iframe中>
< / body>

Frame.html -

 < a href =http://www.google.comid =hi>测试< / a> 

它不会返回警告框。但是在Internet Explorer上它会。我一直在寻找互联网,尝试所有的例子,并找不到一个简单的例子,可以在Google Chrome中使用。我做错了什么,或者它只是谷歌浏览器?

解决方案

我注意到 contentDocument getSVGDocument (对于来自svg文件的svg对象)如果代码来自Web服务器,则工作正常,但是当我将代码复制到一个本地文件,它将无法正常工作。



下面是一个示例链接,代码是我从网上找到的代码,而不是从我的磁盘上运行。



下面是我刚刚从Chrome上发现的解决方案 (感谢 Artem S ),即使用 - allow-file-access- from-files 标志。


When I try my code out:

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>

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?

解决方案

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.

Here's the solution I just found from Chrome (thanks to Artem S), i.e. using the --allow-file-access-from-files flag.

这篇关于谷歌浏览器不会接受.contentDocument或.contentWindow!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆