Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-24-2012, 09:41 PM   PM User | #1
hadoob024
New Coder

 
Join Date: Apr 2008
Posts: 43
Thanks: 5
Thanked 0 Times in 0 Posts
hadoob024 is an unknown quantity at this point
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???
hadoob024 is offline   Reply With Quote
Old 09-24-2012, 10:18 PM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,172
Thanks: 59
Thanked 3,993 Times in 3,962 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 09-24-2012, 10:21 PM   PM User | #3
hadoob024
New Coder

 
Join Date: Apr 2008
Posts: 43
Thanks: 5
Thanked 0 Times in 0 Posts
hadoob024 is an unknown quantity at this point
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..
hadoob024 is offline   Reply With Quote
Old 09-24-2012, 11:24 PM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,172
Thanks: 59
Thanked 3,993 Times in 3,962 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 09-25-2012, 12:14 AM   PM User | #5
hadoob024
New Coder

 
Join Date: Apr 2008
Posts: 43
Thanks: 5
Thanked 0 Times in 0 Posts
hadoob024 is an unknown quantity at this point
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.
hadoob024 is offline   Reply With Quote
Old 09-25-2012, 01:02 AM   PM User | #6
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,172
Thanks: 59
Thanked 3,993 Times in 3,962 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 09-25-2012, 01:06 AM   PM User | #7
hadoob024
New Coder

 
Join Date: Apr 2008
Posts: 43
Thanks: 5
Thanked 0 Times in 0 Posts
hadoob024 is an unknown quantity at this point
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.
hadoob024 is offline   Reply With Quote
Old 09-25-2012, 01:30 AM   PM User | #8
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,172
Thanks: 59
Thanked 3,993 Times in 3,962 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 09-25-2012, 01:32 AM   PM User | #9
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,172
Thanks: 59
Thanked 3,993 Times in 3,962 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 09-25-2012, 12:15 PM   PM User | #10
hadoob024
New Coder

 
Join Date: Apr 2008
Posts: 43
Thanks: 5
Thanked 0 Times in 0 Posts
hadoob024 is an unknown quantity at this point
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.
hadoob024 is offline   Reply With Quote
Old 09-25-2012, 04:49 PM   PM User | #11
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,172
Thanks: 59
Thanked 3,993 Times in 3,962 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 09-25-2012, 06:48 PM   PM User | #12
hadoob024
New Coder

 
Join Date: Apr 2008
Posts: 43
Thanks: 5
Thanked 0 Times in 0 Posts
hadoob024 is an unknown quantity at this point
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!
hadoob024 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:29 AM.


Advertisement
Log in to turn off these ads.