Jack
Jack

Reputation: 2625

Reading Parent's URL

I have an iFrame that is running some Javascript and I want the iFrame to behave differently depending on which page it is loaded into. I found this code which works brilliantly but it shows me the url of the iFrame not the parent.

 var Page1 = "page1.html";
 var Page2 = "page2.html";
 var thisUrl = decodeURI(window.location);
 var urlChunks = thisUrl.split("/");

for (var chunk in urlChunks) {
alert('chunk: ' + chunk);
alert('urlChunks[chunk]: ' + urlChunks[chunk]);

if (urlChunks[chunk] == Page1) {
alert('inside index.html');

}

else if (urlChunks[chunk] == Page2) {

}

else
{

}

}

What can I change the

decodeURI(window.location);

to in order to get it to read from the parent.

Upvotes: 0

Views: 176

Answers (1)

Rafael
Rafael

Reputation: 18522

window.parent.location

Remember that JavaScript has the Same-Origin restriction, so when the parent document is a different origin (eg. domain), you will most probably get an access-denied exception.

Upvotes: 1

Related Questions