My problem is that I'm creating an application that will be added to different pages in an iframe. But sometimes it will be on my own domain and sometimes on some one elses. And when it is hosted on my own domain I want to be able to call functions outside the frame.
Is it possible to check if the parent window is on the same domain (with javascript/jQuery)? At the moment I get this ugly error when trying to access somethin outside my frame when not hosted on my own site: "Error: Permission denied to access property 'document'"
Wanna do something like this:
if(window.parent is accessible) {
// Do special stuff
} else {
// Do nothing
}
My problem is that I'm creating an application that will be added to different pages in an iframe. But sometimes it will be on my own domain and sometimes on some one elses. And when it is hosted on my own domain I want to be able to call functions outside the frame.
Is it possible to check if the parent window is on the same domain (with javascript/jQuery)? At the moment I get this ugly error when trying to access somethin outside my frame when not hosted on my own site: "Error: Permission denied to access property 'document'"
Wanna do something like this:
if(window.parent is accessible) {
// Do special stuff
} else {
// Do nothing
}
Share
Improve this question
asked Oct 24, 2013 at 12:19
KungWazKungWaz
1,9563 gold badges42 silver badges65 bronze badges
3
- Make some search about crossdomain policies in JS... – JoDev Commented Oct 24, 2013 at 12:21
- 2 Can't find anything relevant, that's why I posted here... – KungWaz Commented Oct 24, 2013 at 12:36
- 1 First result in my first search : stackoverflow and the result would be founded in this site – JoDev Commented Oct 24, 2013 at 12:38
2 Answers
Reset to default 18You could use:
try{
parent.document;
// accessible
}catch(e){
// not accessible
}
I tried checking parent document, but for some reasons in new iOS devices (chrome or safari) it didn't throw the exception, but just continued to the next step.
So I took it another step:
try{
var doc = parent.document;
if(!doc)
throw new Error('Unaccessible');
// accessible
}catch(e){
// not accessible
}