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'
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.
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");
}
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]
contentWindow.location? Does that not give you the same results as.href?