change window location when iframe location is cha

2019-08-26 05:12发布

问题:

Is there a way to make it so that if the page inside an iframe is not the src, then it will change the parent page to the url the iframe is trying to go to? Something like this:

<script>
function redir() {
    if(document.getElementById("myiframe").src != 'http://www.google.com'){
        window.location = document.getElementById("myiframe").location
    }
}
</script>
<iframe src="http://www.google.com/" id="google" onload="redir()"></iframe>

回答1:

No.

Per the same-origin policy, the parent page is not allowed to get any information about what's going in the iframe, unless the pages are on the same domain.