繁体   English   中英

在window.open之后尝试从父窗口调用子窗口函数

[英]Trying to call the child window function from parent window after window.open

我正在尝试调用以下函数来创建一个新窗口。 无论何时,我都在调用它使chrome父窗口崩溃。

我观察到的是:直到执行了openDoctorDetails函数,才完全不加载子窗口。 因此,从未定义doctorDetailsPage.populateDoctorDetails 因此,它进入了无限循环。

var openDoctorDetails = function(physicianId) {
    var time1 = new Date();
    var doctorDetailsPage = window.open('PhysicianDetails.html', '_blank',
        'resizable=yes; width=1000; height=1500; top=50; left=50');
    var times = 0;
    while (!doctorDetailsPage.populateDoctorDetails) {
    setTimeout(function() {
        times++;
    }, 500);
    }
    doctorDetailsPage.populateDoctorDetails(physicianId);
    doctorDetailsPage.focus();
    console.log("Time taken for this openDoctorDetails:" + (new Date() - time1)
        + "  " + times);
};

在子窗口中实现onload事件处理程序,该处理程序在父窗口中调用physicianDetailsWindowLoadedHandler方法。 不要使用setTimeout。 只需打开子窗口,并让直接回调以事件驱动的方式处理所有事情。

这样,代码将在子窗口加载后在子窗口中运行,而无需创建自己的等待循环,并且如果子窗口需要来自父窗口的信息,则可以通过在父窗口中调用方法来访问代码通过window.parent属性。

暂无
暂无

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

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