Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    New Coder
    Join Date
    Apr 2008
    Posts
    43
    Thanks
    5
    Thanked 0 Times in 0 Posts

    window.open() not getting executed in IE (working in Firefox)

    In a popup, I have a button. Here's how I defined the button:

    Code:
    <input type="button" name="generatePO" value="Z-PO" onclick="JavaScript:window.close();window.location='index.php?module=Patient_Procedure&action=genMFGPO'&pomode=create&kindofpo=Z';window.open(\'index.php?module=Patient_Procedure&action=genMFGPO&pomode=preview','poverifywin','width=1250,height=1000,status=0,resizable=1,scrollbars=1,toolbar=0,menubar=0,location=0')">
    Basically, what it does is, close the popup, execute that window.location call which creates a PDF, then executes window.open() to open another popup to see if there are any more PDF's that can be created. This works fine in Firefox, but that window.open() never gets executed in IE 9.

    Tried some google searches, but nothing came up.

    I've rewritten this several times and can't get it to work in IE 9. Here's one of my rewrites:

    Code:
    <input type="button" name="generatePO" value="Z-PO" onclick="JavaScript:window.location='index.php?module=Patient_Procedure&action=genMFGPO'&pomode=create&kindofpo=Z';window.location.replace('index.php?module=Patient_Procedure&action=genMFGPO&pomode=preview');">
    For some reason though, the window.open() doesn't get executed unless I click on the button twice. Can't think of how to fix this for IE. Thoughts???

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,184
    Thanks
    75
    Thanked 4,340 Times in 4,306 Posts
    Code makes no sense.

    Code:
    window.close(); // the CURRENT WINDOW is now closed...NO OTHER CODE can use the current window
    
    // so this next line is meaningless, since the current window no longer exists.
    window.location='index.php?module=Patient_Procedure&action=genMFGPO'&pomode=create&kindofpo=Z';
    
    // this next code might work, but it might not...
    // ...because once the current window is closed where is the JS code supposed to be working?
    window.open(\'index.php?module=Patient_Procedure&action=genMFGPO&pomode=preview','poverifywin','width=1250,height=1000,status=0,resizable=1,scrollbars=1,toolbar=0,menubar=0,location=0')
    If you want to close the current popup, that must be the *LAST* thing you do.

    But what I don't understand is where the PDF file you say the middle statement opens is supposed to go if you are (a) trying to put it into the current window [which is what window.location will do] and (b) trying to close the current window.

    That just makes no sense, at all.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    New Coder
    Join Date
    Apr 2008
    Posts
    43
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    Code makes no sense.

    Code:
    window.close(); // the CURRENT WINDOW is now closed...NO OTHER CODE can use the current window
    
    // so this next line is meaningless, since the current window no longer exists.
    window.location='index.php?module=Patient_Procedure&action=genMFGPO'&pomode=create&kindofpo=Z';
    
    // this next code might work, but it might not...
    // ...because once the current window is closed where is the JS code supposed to be working?
    window.open(\'index.php?module=Patient_Procedure&action=genMFGPO&pomode=preview','poverifywin','width=1250,height=1000,status=0,resizable=1,scrollbars=1,toolbar=0,menubar=0,location=0')
    If you want to close the current popup, that must be the *LAST* thing you do.

    But what I don't understand is where the PDF file you say the middle statement opens is supposed to go if you are (a) trying to put it into the current window [which is what window.location will do] and (b) trying to close the current window.

    That just makes no sense, at all.

    By meaningless, do you mean that it won't run, because it does. The window.location does execute, it's just the final window.open() that doesn't run. I didn't write this, just trying to fix it so that it runs in IE. This code runs fine in Firefox.

    Also, I'm not displaying the PDF, just generating it and saving it locally.
    Last edited by hadoob024; 09-24-2012 at 10:36 PM.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,184
    Thanks
    75
    Thanked 4,340 Times in 4,306 Posts
    It's a question of timing: It is completely dependent on WHEN things happen. In Firefox, apparently both of the calls happen before the window.close() completes. In MSIE, clearly that doesn't happen.

    Once you call window.close() on the current window, there's no guarantee what code, if any, will run from that window. For that matter, once you do window.location, there is no guarantee any code from the prior location will be able to run.

    I would suggest that the way to do this would be to use AJAX, instead of window.location, to generate the PDF file (and you can just ignore the return from the AJAX call, then), then do the window.open() for the new popup, then do the window.close().

    By calling AJAX, you aren't depending on any timing of events.

    And not to ask a silly question, but...instead of doing the window.open to create a new popup and then closing the current one, why not just use window.location to replace the current popup with the new one?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    New Coder
    Join Date
    Apr 2008
    Posts
    43
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    It's a question of timing: It is completely dependent on WHEN things happen. In Firefox, apparently both of the calls happen before the window.close() completes. In MSIE, clearly that doesn't happen.

    Once you call window.close() on the current window, there's no guarantee what code, if any, will run from that window. For that matter, once you do window.location, there is no guarantee any code from the prior location will be able to run.

    I would suggest that the way to do this would be to use AJAX, instead of window.location, to generate the PDF file (and you can just ignore the return from the AJAX call, then), then do the window.open() for the new popup, then do the window.close().

    By calling AJAX, you aren't depending on any timing of events.

    And not to ask a silly question, but...instead of doing the window.open to create a new popup and then closing the current one, why not just use window.location to replace the current popup with the new one?
    Cool. Thanks for the tips!

    Honestly, I didn't know about it

    I'm more of a backend programmer than a front-end developer. Just found out about that window.location.replace yesterday.

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,184
    Thanks
    75
    Thanked 4,340 Times in 4,306 Posts
    Okay, can you figure out how to do the AJAX call? If not, I'll show you. Not hard.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    New Coder
    Join Date
    Apr 2008
    Posts
    43
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Cool. Yeah I do, thanks. The way I'm doing it right now is more of a band-aid fix. I'll probably rewrite it using some jQuery when time allows.

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,184
    Thanks
    75
    Thanked 4,340 Times in 4,306 Posts
    If you are not using jQuery for other things on the page, it's not worth dragging in that whole library for something as simple as this.

    Code:
    // use AJAX to start creation of the new PDF
    var http = window.XMLHttpRequest != null 
                ? new XMLHttpRequest() 
                : new ActiveXObject("Microsoft.XMLHTTP");
    http.open("GET","index.php?module=Patient_Procedure&action=genMFGPO'&pomode=create&kindofpo=Z",true);
    http.send( ); 
    // ignore the asynch response
    
    // then just change the current popup to the new location:
    location.href = "index.php?module=Patient_Procedure&action=genMFGPO&pomode=preview";
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #9
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,184
    Thanks
    75
    Thanked 4,340 Times in 4,306 Posts
    And if you don't care about MSIE 7, it can be even simpler:
    Code:
    // use AJAX to start creation of the new PDF
    var http = new XMLHttpRequest();
    http.open("GET","index.php?module=Patient_Procedure&action=genMFGPO'&pomode=create&kindofpo=Z",true);
    http.send( ); 
    // ignore the asynch response
    
    // then just change the current popup to the new location:
    location.href = "index.php?module=Patient_Procedure&action=genMFGPO&pomode=preview";
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #10
    New Coder
    Join Date
    Apr 2008
    Posts
    43
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    And if you don't care about MSIE 7, it can be even simpler:
    Code:
    // use AJAX to start creation of the new PDF
    var http = new XMLHttpRequest();
    http.open("GET","index.php?module=Patient_Procedure&action=genMFGPO'&pomode=create&kindofpo=Z",true);
    http.send( ); 
    // ignore the asynch response
    
    // then just change the current popup to the new location:
    location.href = "index.php?module=Patient_Procedure&action=genMFGPO&pomode=preview";
    Cool. Yeah, this might work even better. It's an in-house app, and we're moving to IE 9, so no need to code for IE 7.

    Quick question, that script that builds the PDF takes a couple of seconds to run. Is there something that I could do so that after the user clicks on the button, they don't click on it again until the whole process runs? I wonder if that's why the window.close was put in.

  • #11
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,184
    Thanks
    75
    Thanked 4,340 Times in 4,306 Posts
    Lots of things you could do...how complex do you want to get?

    You could always go back to using the popup and close; just make sure the close comes after the call to create the popup.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #12
    New Coder
    Join Date
    Apr 2008
    Posts
    43
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    Lots of things you could do...how complex do you want to get?

    You could always go back to using the popup and close; just make sure the close comes after the call to create the popup.
    This fix is more of a "gotta have it now" instead of "gotta do it right".

    I just decided to disable the button as the first call before generating that PDF. That seems to work! Thanks again!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •