admin管理员组

文章数量:1291102

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="" id="hi">Testingz</a></iframe>
</body>

Frame.html -

<a href="" 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?

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." id="hi">Testingz</a></iframe>
</body>

Frame.html -

<a href="http://www.google." 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?

Share Improve this question edited Mar 31, 2020 at 12:05 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Mar 17, 2011 at 1:33 jhfirejhfire 411 silver badge2 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 7

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.

Try it without the .document

var form = iframeEl.contentDocument.getElementById('hi').getAttribute('href');

instead of

var form = iframeEl.contentDocument.document.getElementById('hi').getAttribute('href');

The answer from @IsraelGav is correct in the sense that this problem occurs when the code is accessed from a local file but not when accessed from a web server. It is also correct in the sense that using the --allow-file-access-from-files flag can allow the local file to be accessed by Chrome.

However, it misses an important security concern here. Both the concern, as well as an alternative possible solution, were originally described in this other SO answer by @orszaczky. To summarize the alternative solution: On Windows, install http-server (npm install -g http-server) and run http-server from your project directory. On Mac/Linux, run python -m SimpleHttpServer from your local directory. You can now access the locally hosted web site in your browser. On Windows I had to use localhost:8080 while on the Mac I had to use localhost:8000.

By the way, this is not only an issue for Chrome (v49.0) but also for Opera (v35.0), on both Windows and Mac.

本文标签: javascriptGoogle Chrome Won39t Accept contentDocument or contentWindowStack Overflow