...

View Full Version : window.open() not getting executed in IE (working in Firefox)



hadoob024
09-24-2012, 09:41 PM
In a popup, I have a button. Here's how I defined the button:


<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,me nubar=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:


<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???

Old Pedant
09-24-2012, 10:18 PM
Code makes no sense.



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,me nubar=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.

hadoob024
09-24-2012, 10:21 PM
Code makes no sense.



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,me nubar=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.

Old Pedant
09-24-2012, 11:24 PM
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?

hadoob024
09-25-2012, 12:14 AM
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.

Old Pedant
09-25-2012, 01:02 AM
Okay, can you figure out how to do the AJAX call? If not, I'll show you. Not hard.

hadoob024
09-25-2012, 01:06 AM
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.

Old Pedant
09-25-2012, 01:30 AM
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.



// 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";

Old Pedant
09-25-2012, 01:32 AM
And if you don't care about MSIE 7, it can be even simpler:


// 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";

hadoob024
09-25-2012, 12:15 PM
And if you don't care about MSIE 7, it can be even simpler:


// 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.

Old Pedant
09-25-2012, 04:49 PM
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.

hadoob024
09-25-2012, 06:48 PM
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!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum