Sometimes we require to show the print dialog on as soon as we load the Web Page, the following piece of script can achieve the same, simply paste the script block on the page and you will see the Windows print dialog when you load the webPage.

<script type="text/javascript">
        function PrintWindow()
        {                    
           window.print();            
           CheckWindowState();
        }
       
        function CheckWindowState()
        {           
            if(document.readyState=="complete")
            {
                window.close(); 
            }
            else
            {           
                setTimeout("CheckWindowState()", 2000)
            }
        }    
        
       PrintWindow();
</script>
--------------------------------------------------------------------------------------------------------------------------------------

The readyState property indicates the current state of the XML document.

This property is read-only and returns one of the following values indicating the instantiation and download state of the XML document.

  • LOADING
    loading in progress; reading persisted properties but not yet started parsing data. For purposes of the standard readyState definitions, data should be considered equivalent to BLOB properties.
  • LOADED
    reading of persisted properties completed; reading and parsing data, but the object model is not yet available.
  • INTERACTIVE
    some data has been read and parsed, and the object model is available on the partially received data set. The object model at this stage is read-only.
  • COMPLETED
    the document has been completely loaded, successfully or unsuccessfully.

This property is a Microsoft extension to the W3C DOM.


Feedback

# re: Print Window On Load

Hi,
I tried this code. It doesn`t work in firefox.
3/21/2008 12:45 PM | Filip

# re: Print Window On Load

Worked great for me IE and Firefox. Firefox didn't close after the print so I added a close link to the page. with this:

<CENTER><FONT color=#800000 size=-1>Click to close This Page</FONT></CENTER> 3/20/2009 2:02 AM | Jerome

# re: Print Window On Load

fdgbfdg gfdgfdg gfdg 2/22/2010 11:54 AM | vfdgdg dfgrfgg

# re: Print Window On Load

we wewe e ewr ew 2/22/2010 11:55 AM | sacsds sfafdsf

# re: Print Window On Load

This code doesn't work in either IE or firefox. Is there anything that we need to know to make it work?

In my case I am loading a pdf document on to an aspx page in code behind as below.
*********************************************
Imports System.IO

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim byteContent As Byte() = Nothing
If Not IsPostBack Then 'coming for the first time
Dim strFilename As String = Request.QueryString("a").ToString
If strFilename.Trim.Length > 0 Then
If File.Exists(strFilename) Then

byteContent = File.ReadAllBytes(strFilename)

If InStr(UCase(Request.ServerVariables("HTTP_USER_AGENT")), "NETSCAPE") > 0 Then
Response.AddHeader("Content-disposition", "attachment; filename=" & strFilename)
End If

Response.ContentType = "application/pdf"
Response.AddHeader("content-disposition", "inline; filename=" & strFilename)
Response.BinaryWrite(byteContent)
End If
End If
'Page.ClientScript.RegisterStartupScript(Me.[GetType](), "Print", "javascript:window.print();", True)
End If

Response.Flush()
Response.Close()
Response.End()

End Sub
************************************************

Then in aspx page, I have
***********************
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body bottommargin="0" leftmargin="0" topmargin="0" rightmargin="0">
<form id="form1" runat="server">
<div>

</div>
</form>
<script language="javascript">
<!--
window.print();
//-->
</script>
<script language="javascript">
<!--
function PrintWindow() {
location.reload(true);
window.print();
CheckWindowState();
}

function CheckWindowState() {
if (document.readyState == "complete") {
window.close();
}
else {
setTimeout("CheckWindowState()", 2000)
}
}

PrintWindow();
//-->
</script>

</body>
</html>
**********************
PDF file loads fine and the page comes up, but with no print dialog popup.

Appreciate any ideas on this.
thanks 8/10/2010 7:45 PM | Vijaya

Post a comment





 

News

Employers
Soppa Group India
iSmart Panache Inc
R Systems Internationals Ltd
Technovate eSolutions Pvt Ltd
The contents of this blog are my personal opinion and do not represent in any way the view of my employer.
These postings are provided "AS IS" with no warranties, and confer no rights.

Google PR™ - Post your Page Rank with MyGooglePageRank.com

Archives

Post Categories

Image Galleries

Articles & Magazines

ASP.Net 2.0 Compilation

ASP.Net, Blogs I refer...

Atlas

Dost

Drivers and Software Download

Garhwal

Travel Domain

WSS and WebParts

Syndication: