eozzy
eozzy

Reputation: 68650

Getting the URL from the address bar

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

Answers (5)

shyammakwana.me
shyammakwana.me

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

TBAR
TBAR

Reputation: 448

Try something like this

var url = (window.location != window.parent.location) ? document.referrer: document.location;

Upvotes: 4

Shai Aharoni
Shai Aharoni

Reputation: 1957

Try

var myIframe = document.getElementById('yourIframeId');

alert(myIframe.contentWindow.location.hostname);

Upvotes: -1

Mihai Stancu
Mihai Stancu

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

QBM5
QBM5

Reputation: 2788

var URL = window.location.href;

Upvotes: -1

Related Questions