2016-12-12 226 views
0

我想调用下面的函数来创建一个弹出的新窗口。无论何时,我都在调用它在chrome中崩溃父窗口。试图在window.open后从父窗口调用子窗口函数

我观察到:直到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); 
}; 
+0

http://stackoverflow.com/questions/13311812/popup-window-accessing-parent-dom – Mikes3ds

回答

0

实现调用父窗口physicianDetailsWindowLoadedHandler方法的子窗口的onload事件处理程序。放下setTimeout业务。只需打开子窗口,让回调以事件驱动的方式处理所有事情。

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

+0

如何获得parentId,如果它是动态的?在父窗口中创建隐藏变量不是一种选择,因为我可能需要一次创建多个窗口。我不想要任何并发问题。 – krishth

+0

你有几个选项。例如,将具有标识符(或父窗口中的集合中的索引)的查询参数添加到您打开窗口的URL中。或者在'window.open()'方法中使用name参数。另外,如果打开窗口的调用成功,返回值将是对窗口的引用。因此,在子窗口的onload事件处理程序中,可以在父窗口中调用回调方法,将'window'作为参数传递,并且回调方法可以查找子窗口集合。 – Craig

+0

有没有什么办法可以将参数从父母传递给孩子。 – krishth

相关问题