如何使用JavaScript从父窗口获取子窗口URL [英] How to get child window url from parent window using javascript

查看:71
本文介绍了如何使用JavaScript从父窗口获取子窗口URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在启动一个带有窗口引用名称的子窗口.我想在每次更改时捕获子窗口的URL.


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);
    }


我尝试使用事件LOAD,CHANGE捕获窗口的位置更改.但是LOAD和CHANGE事件仅触发一次.

我不想使用setinterval检查window.location.href中的更改,因为这可能会导致性能问题.

还有其他方法可以获取子窗口URL吗?


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?

推荐答案

您无法精确地检测到该事件,但是可以采取一种解决方法,并在所有X毫秒内检查URL.

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

这篇关于如何使用JavaScript从父窗口获取子窗口URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆