2
alert(document.getElementById('external-site').contentWindow.location.href);

Hello all, this code is working in Chrome and showing "undefined" but in Mozila Firefox it is showing error.

Error: Permission denied to access property 'href'

4
  • What about just contentWindow.location? Does that not give you the same results as .href? Commented May 30, 2012 at 7:17
  • How can you say this is working for Chrome when it shows 'undefined'? Is that what you expect? Commented May 30, 2012 at 7:24
  • @mattytommo after removing .href it is showing Error: Could not convert JavaScript argument arg 0 [nsIDOMWindow.alert] Commented May 30, 2012 at 7:28
  • @Jochem yep if it is external site url then i need undefined. if the url in iframe is of current site then it is working correct, Commented May 30, 2012 at 7:29

3 Answers 3

2

if document.getElementById('external-site') is referring to an iframe which is loading a page from a different domain then firefox runs into a same-origin policy error and you cannot access to window.location object

From MDN:

The same origin policy prevents a document or script loaded from one origin from getting or setting properties of a document from another origin. This policy dates all the way back to Netscape Navigator 2.0.

Sign up to request clarification or add additional context in comments.

3 Comments

Ya page is of diffrent domain. But is there something else i can do my work if the url in iframe is of current site url?
in a such case you won't get any error. Remember that same domain means same protocol + same domain (and subdomains if any) + same port
But how can i check if the current url of iframe is my site? because after some process on other site it will redirect to my site in iframe and i want to change the window location to current location of iframe.
0

Since all you want to do is 'check' if it is external, why not catch the error? Seems to work in FF and Chrome. See jsFiddle.

try {
    alert(document.getElementById('external-site').contentWindow.location.href);
}
catch (err) {
    alert("undefined");
}

4 Comments

yeah but if the domain in iframe is diffrent from domain of site then it always will show javascript error in console. So that i just need a method that is enable if only iframe have same site url. please tell if any method like this is available?
Did you try my fiddle? If the domain is different, 'alert("undefined")' will be executed and NO error ends up in the console. If the url links to the same site (as the containing page), the code within the try {} will execute. These two use cases are actually in the jsFiddle. [edit: at least, this is what happens in my versions of Chrome and Firefox]
yes but i know but i dont want to use any try catch block for this ;) but but i think there is no more solution for this so i do this. ;) thnx.. :)
Feel free to accept an answer, this may also help you get future questions answered.
0

I was also getting the same error.

To overcome this error I used the following line of code:

alert(document.getElementById('external-site').contentWindow.document.location.href);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.