Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cross browser alternative to window.close() 2

Status
Not open for further replies.

ChrisRChamberlain

Programmer
Mar 23, 2000
3,392
0
0
GB
Hi all

The following code works in IE, but in Firefox, Safari, Opera and Chrome the 'window.close' function does not work:-
Code:
<body onLoad="if(confirm('Do you want to print this page?'))window.print();window.close();">
Any suggestions, please?

TIA

FAQ184-2483​
Chris [pc2]
PDFcommander.com
motrac.co.uk
 
Try self.close instead of window.close

If you want the best response to a question, please check out FAQ222-2244 first.
'If we're supposed to work in Hex, why have we only got A fingers?'
Drive a Steam Roller
 
Scripts may not close windows that were not opened by script.

~Firefox's JavaScript console in response to a window.close event.

Looks like some security setting. I don't see an option in Firefox's GUI to override this. So the options seem to be:

A) Have your window be opened by JavaScript (if you just do <a target="_blank">, should be simple to replace that with onClick="window.open"...)

B) Tell your users to edit about:config and change their settings there to allow scripts to close windows.

C) Deal with it.

Cuvou.com | My personal homepage
Code:
perl -e '$|=$i=1;print" oo\n<|>\n_|_";x:sleep$|;print"\b",$i++%2?"/":"_";goto x;'
 
johnwm

Thanks for your reply and suggestion - unfortunately it does not work.

Kirsle

Thanks for your reply which is under investigation.

By way of explanation, the user clicks on a link on a page containing multiple product specifications. The page to be printed has a single product specification and once printed the user should return whence he/she came.

The following code would be preferable if it could be modified to bring up the print dialog before the second query of the user.

It works in all browsers previously listed.
Code:
<body onLoad="if(confirm('Do you want to print this page?'))window.print();if(confirm('Do you want to return to the previous page?'))history.go(-1);">

FAQ184-2483​
Chris [pc2]
PDFcommander.com
motrac.co.uk
 
Kirsle

Have used your suggestion as well as johnwm's and called the page to print with 'onClick="window.open"' etc.

The code in the called page is now
Code:
<body onLoad="window.print();self.close();">
Works in the browsers previously listed. [smile]

Thanks both again.

FAQ184-2483​
Chris [pc2]
PDFcommander.com
motrac.co.uk
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top