I have a page in which I'm including iframes from two different domains i.e. like below
<!-- domain 1 frames -->
<iframe src="domain1/page1.html">
<iframe src="domain1/page2.html">
<!-- domain 2 frames -->
<iframe src="domain2/page1.html">
Now let's say I have the below javascript code in domain1/page1.html
var frames = window.parent.frames;
This will give me all iframes (iframe from domain 1 & domain 2) from parent windows. However, I want to get only iframes from domain 1. When I try to filter iframes from only domain 1 using the below code
var isSameOrigingIframe = window.location.host == window.parent.frames[i].location.host;
I receive a security exception (Cross-origin policy violation) as an iframe from domain 1 tries to access iframe domain 2.
My current solution relies on try-catch block as below
var isSameOrigingIframe = false;
try{
isSameOrigingIframe = window.location.host == window.parent.frames[i].location.host;
}catch(e){
isSameOrigingIframe = false;
}
The above solution works but is there is any safe way to get iframes only from the same domain? Or there is a better way to filter out iframes from the same domain.