Wednesday 21 November 2007

The web page you are viewing is trying to close the window

Today i added print functionality to a web page that basically changes the stylesheet for print formatting and also automatically fires the print dialogue for the user. On doing this in IE7 you get the message:

"The web page you are viewing is trying to close the window"

when running the follwing javascript added to the page on page load:

If Not ClientScript.IsStartupScriptRegistered(Me.GetType, "OnLoad") Then

ClientScript.RegisterStartupScript(Me.GetType, "OnLoad", "window.print();window.close();", True)

End If

All you need to do to counteract this is put the following javascript code in before the close:

window.open('','_self');

So the code will now look like this:

ClientScript.RegisterStartupScript(Me.GetType, "OnLoad", "window.print();window.open('','_self');window.close();", True)

Works a treat.

15 comments:

Anonymous said...

Great work

Only from this place i found this solution.
Thank you verymuch.

Gayan Krishantha
(krishanthaene@gmail.com)

Krishantha said...

Great work

Only from this place i found this solution.
Thank you verymuch.

Gayan Krishantha
(krishanthaene@gmail.com)

Anonymous said...

Works perfectly well for me.
Thank you very much.
Take care.

Anonymous said...

Worked for me, too (IE8). Thank you very much!

MikeN

Anonymous said...

Works perfectly! Other pages had solutions to this which didn't work for me.
ROL

Ram said...

Awesome. it worked great. Thanks.

Unknown said...

Hi, I have IE8 and I get access denied on the window.open('','_self','');

I tried also window.opener=self or other strings but then in this case I still get the popup message before it closes...

Any idea what might be wrong? Thanks

Unknown said...

Excellent Work.
Thank you soo much...
This is the only place i found the solution for this.

Thanks & Regards.
P.Shiva Krishna

Unknown said...

Excellent Work.
This the only place i found solution for this.

Thank you soo much..

Regards..
P.Shiva Krishna

Unknown said...

This worked perfectly. I wish this showed up first in a Google search. Thank you so very much!

If I'm understanding this correctly, the window is essentially taking control of itself before closing, thus suppressing the silly popup.

Also @Christian
You said you get access denied on window.open('','_self',''); but I don't think we're calling it with a third parameter. Try just window.open('','_self');

Unknown said...

Well done, Great work. Thanks

Anonymous said...

Hi dear!

Amazing wok.

I realy found it at this page
It works fine.

Thanks


Qaiser yaqoob

Anonymous said...

Fantastic. Thanks very much!

Anonymous said...

Works beautifully!, thanks for sharing!

Anonymous said...

Perfect! Worked like a charm. Thanks!