Javascript: Can't access iframe document - Stack Overflow

I'm just trying to pull an image from a site (not necessarily Bing but the problem is with every s

I'm just trying to pull an image from a site (not necessarily Bing but the problem is with every site it seems).
Running this code it seems to have failed at the 'if' statement. This makes me think there is no document.
I've been trying to figure out why this won't work but with my limited HTML/Javascript knowledge I just can't. Can someone enlighten me please.

I wouldn't need this many variables normally but in order to figure out the problem I made this code. I've also tried window.frames[0].document with no luck.

HTML:
<iframe id="test" src=""></iframe> <button onclick="myFunciton()">Go</button>

Javascript:

function myFunction() {
var x = document.getElementById("test");
var y = (x.contentWindow || x.contentDocument);
if (y.document){y = y.document;}
var test = y.getElementsByTagName("img")[0].src;
}

I'm just trying to pull an image from a site (not necessarily Bing but the problem is with every site it seems).
Running this code it seems to have failed at the 'if' statement. This makes me think there is no document.
I've been trying to figure out why this won't work but with my limited HTML/Javascript knowledge I just can't. Can someone enlighten me please.

I wouldn't need this many variables normally but in order to figure out the problem I made this code. I've also tried window.frames[0].document with no luck.

HTML:
<iframe id="test" src="https://www.bing./images/search?q=test"></iframe> <button onclick="myFunciton()">Go</button>

Javascript:

function myFunction() {
var x = document.getElementById("test");
var y = (x.contentWindow || x.contentDocument);
if (y.document){y = y.document;}
var test = y.getElementsByTagName("img")[0].src;
}
Share Improve this question asked Jul 8, 2018 at 9:18 AlexAlex 692 silver badges6 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 2

The problem is not with your if statement. For me, your code gives me back an error:

SecurityError: Blocked a frame with origin "https://www.example." from accessing a frame with origin "https://www.bing.". Protocols, domains, and ports must match. 

Which means that you simply cannot access an iframe with different origins, otherwise it would be a huge security issue. The only way of interacting between iframes is using window.postMessage which you can read about in another post on StackOverflow.

You should be able to access iframe content by using following:

parent.ifr.document.getElementById('<some id>');

where ifr is the name or id of iframe window.

Please, also mind the cross-domain issues.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745272394a4619828.html

相关推荐

  • Javascript: Can&#39;t access iframe document - Stack Overflow

    I'm just trying to pull an image from a site (not necessarily Bing but the problem is with every s

    1小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信