简体   繁体   English

如何使用javascript从父窗口获取子窗口URL

[英]How to get child window url from parent window using javascript


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. 我想在每次更改时捕获子窗口的URL。

    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. 我尝试过事件LOAD,CHANGE来捕获窗口的位置变化。 But the LOAD and CHANGE events gets triggered only once. 但LOAD和CHANGE事件只触发一次。

I dont want to use setinterval to check for the change in window.location.href as it may lead to performace issues. 我不想使用setinterval来检查window.location.href中的更改,因为它可能会导致性能问题。

Is there any other way around to get the child window URL? 有没有其他方法来获取子窗口URL?

You can't detect the event precisely, but you can do a workaround and check the URL all X milliseconds. 您无法准确检测事件,但您可以执行一种解决方法并检查所有X毫秒的URL。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM