Thursday, June 20, 2013

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.

Ad Inside Post

Comments system

Disqus Shortname