Reputation: 68650
I have an iframe on mynewwebsite.com
showing content from mywebsite.com
alert(window.location.hostname);
always shows mywebsite.com
, is there any way I could get the URL from the user's address bar without modifying anything on mynewwebsite.com
?
I want to check the domain and load stylesheets accordingly.
Upvotes: 1
Views: 223
Reputation: 5752
Maybe this will help:
alert(window.parent.location.hostname);
alert(window.top.location.hostname);
It works in Chrome like a charm.
Upvotes: 0
Reputation: 448
Try something like this
var url = (window.location != window.parent.location) ? document.referrer: document.location;
Upvotes: 4
Reputation: 1957
Try
var myIframe = document.getElementById('yourIframeId');
alert(myIframe.contentWindow.location.hostname);
Upvotes: -1
Reputation: 16107
I think window
object refers to the current window -- the iframe's window object because you're calling it from the iframe.
Try
window.parent.location.hostname
But since they are two different domain names you will encounter a cross-site-scripting security (aka XSS) limitation. You have to configure your servers to allow XSS between the two domains.
Upvotes: 0