3

Scenario is the following:

There's a page and an iframe within that page. Starting out, they're both from the same domain (origin). The iframe can contain links (or javascript that changes window.location.href) to other domains. I want to detect in the outer frame, when user navigates away from the original domain in iframe, to replace outer page's window.location.href with the URL that user navigated to, thus ending the application that was running in the outer frame.

Tried to use beforeunload event but the inner frame's location.href doesn't get changed. Also the iframe DOM element's src property doesn't change when its contentDocument changes.

Is my problem solvable in current browsers implementations or what approach would you use to get closest behaviour to the described one?

Madis Nõmme
  • 1,264
  • 2
  • 15
  • 25
  • Possible duplicate? http://stackoverflow.com/questions/2429045/iframe-src-change-event-detection – bloodyKnuckles Jun 12 '14 at 11:14
  • Not really. I mention in the first paragraph that the src attribute doesn't change when iframe document.location.href is changed from within. So the *'load'* event and *contentDocument.location.href* marked as answers there are not solutions as the iframe navigates to a different domain. – Madis Nõmme Jun 12 '14 at 11:32
  • Are you in control of the initial iframe application? –  Jun 12 '14 at 12:48
  • Yes. My scripts are initially running in both the outer frame and the iframe. – Madis Nõmme Jun 12 '14 at 15:14

1 Answers1

0
    $(document).ready(function () {
                $("#widget").load(function () {
                    var ifr = document.getElementById("widget")
                    var anchors = ifr.contentDocument.getElementsByTagName("a");
                    for (var i in anchors) {
                        anchors[i].setAttribute("onClick", "testUrl()");
                    }
                });
            });  
function testUrl() {   
    //your code goes here for validating the URLs... 
}
sekhar
  • 371
  • 2
  • 10