6


I'm launching a child window with a window reference name. I want to capture the URL of the child window when it changes each time.

    var winRef;
    var url='http://www.google.com';
    if(winRef == null || winRef.closed) 
    { 
      winRef = window.open(url, "mywindow");
      winRef.focus();
      alert(winRef.location.href); 
    }
    winRef.captureEvents(Event.CHANGE);  
    winRef.onchange=function(){
      alert('change event triggered');
      console.log(winRef.location.href);
    }


I have tried with events LOAD, CHANGE to capture the location change of the window. But the LOAD and CHANGE events gets triggered only once.

I dont want to use setinterval to check for the change in window.location.href as it may lead to performace issues.

Is there any other way around to get the child window URL?

6
  • you can't do that from another window, you have to use ajax to send the info to the server or the other window through the server from the child window itself. Commented Jul 4, 2013 at 5:28
  • Phonegap's InAppBrowser has events like loadstart,loadstop which captures the URL everytime some link is clicked. I thought we will be able to do the same from our javascript window.open concept Commented Jul 4, 2013 at 7:53
  • @CME64 I also have the same question. So the child window has to send the message to the server and the server has to send this information to the parent window? Am I correct in my understanding? Commented Apr 6, 2014 at 15:13
  • @SaranyaSukumar I also have the same problem. Could you solve the problem? Commented Apr 6, 2014 at 15:13
  • @Dinesh Yes windows cannot communicate through the browser thus you have to use the only link between them which is the server. You may set the parent page with an ajax poll function (that polls the url every 10 secs for example) and just post the info from the child window to the server to be polled on the next call .. that is if you want to capture the url of the child window and give it to the parent window Commented Apr 13, 2014 at 14:05

1 Answer 1

1

You can't detect the event precisely, but you can do a workaround and check the URL all X milliseconds.

var self = this

var detectsUrl = setInterval(function(){
    var a = winRef.document.createElement('a');
    a.href = winRef.document.URL;
    if (a.hostname == "www.myURL.com") {
        winRef.close();
        clearInterval(detectsUrl);
        self.callback();
    };
}, 1000); // Here we check every seconds
Sign up to request clarification or add additional context in comments.

1 Comment

As much as I like this idea, this is not a viable solution in many cases unless the page of your child window is from the same domain as the parent, otherwise you will get a cross-site security violation exception when you try winRef.document.url

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.