Reputation: 9147
Is it possible test if the page is being iframed by a specific domain with JavaScript?
Like this:
if (window!=window.top){
if (this page is iframed by www.foo.com){ // load foo's special navigation bar}
if (this page is iframed by www.bar.com){ // load bar's special navigation bar}
} else { //load normal navigation bar }
Upvotes: 3
Views: 452
Reputation: 34038
EDIT/UPDATE: Although it is possible to get the domain name of the parent iframe, I didn't want to delete this answer as there is some helpful information for understanding the same domain policy in terms of accessing the DOM. Soumya92's answer proves that an iframe on one domain can indeed retrieve the domain of the parent domain.
However, the other information regarding DOM access still holds true.
Unfortunately, this is just not yet possible in most browsers, and it's definitely not possible unless both websites have given the other domains permission to access each domain's data by setting the document.domain
property. Even then, it will only work with sub-domains.
For instance, foo.example.com
and bar.example.com
can access each other's DOM as long as both HTML pages have set the document.domain
property like so:
// allow foo.example.com and bar.example.com to communicate
document.domain = "example.com";
If you are trying to check to see if www.foo.com
is the parent of a document loaded from www.bar.com
, you will not be able to do this as the iframe will not have access to top.window
.
You could determine if the iframe is being loaded in the same domain, and use powers of deduction to determine that it is not loaded from the same domain when a same domain policy error is encountered.
Beyond that, you will not be able to determine any specifics regarding which domain is loading your document.
Upvotes: -1
Reputation:
Try
var parentLocation='';
if(window.parent && document.referrer)
parentLocation = document.referrer;
alert(parentLocation);
You should see the parent page's url in the alert box. And the rest, as they say, is history...
Upvotes: 3