0

I am trying to invoke the below function to create a new window pop up. Whenver, I am invoking that it is crashing the parent window in chrome.

What I have observed: Till the openDoctorDetails function is executed, the child window is not loaded at all. Because of that doctorDetailsPage.populateDoctorDetails is never defined. Thence, it is going into an infinite loop.

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);
};
krishth
  • 83
  • 1
  • 1
  • 9

1 Answers1

0

Implement an onload event handler in the child window that calls a physicianDetailsWindowLoadedHandler method in the parent window. Don't use setTimeout. Just open the child window and let direct callbacks handle everything in an event-driven manner.

This way, the code will run in the child window after the child window is loaded, without having to create your own wait loop, and if the child window needs information from the parent window, it can access it by calling a method in the parent via the window.parent property.

Craig Tullis
  • 9,939
  • 2
  • 21
  • 21
  • How can I get parentId if it is dynamic? creating a hidden variable in parent window is not an option because I may need to create more than one window at a time. I don't want want any concurrency issues. – krishth Dec 13 '16 at 02:47
  • You have a few options. For instance, add a query parameter with the identifier (or an index into a collection in the parent window) to the URL you open the window with. Or utilize the name argument to the `window.open()` method. Also, if the call to open the window succeeds, the return value will be a reference to the window. So in the child window's onload event handler, you could call the callback method in the parent window, passing `window` as an argument, and the callback method can do a lookup into a collection of child windows. – Craig Tullis Dec 13 '16 at 02:54
  • Is there any way that I can pass arguments from parent to child. – krishth Dec 13 '16 at 19:21
  • I just named two ways: query parameters and the "name" argument to the `window.open(...)` function. ;-) If you mean passing arguments that are direct references back to objects in the parent window, then I wouldn't think so. But you can use the other methods to plumb everything together using callbacks. – Craig Tullis Dec 13 '16 at 20:12