3


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?

  • 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. – CME64 Jul 04 '13 at 05: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 – Saranya Sukumar Jul 04 '13 at 07: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? – Dinesh Apr 06 '14 at 15:13
  • @SaranyaSukumar I also have the same problem. Could you solve the problem? – Dinesh Apr 06 '14 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 – CME64 Apr 13 '14 at 14:05
  • even if you poll using setInterval for window.location.href, you can only do so if the child window opens a same domain url, not cross domain. So in your example, where you are trying to open google.com, you wouldn't be allowed to read the location.href. This is for security reasons – gaurav5430 Nov 20 '20 at 11:41

1 Answers1

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
Tancrede Chazallet
  • 7,035
  • 6
  • 41
  • 62
  • 2
    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 – jcairney Mar 31 '17 at 18:19