Click here to Skip to main content
14,427,818 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi,

I am using Javascript print() function for print the page.

But i Need the after print the page is go back to the previouse page.

I have write following code:

function PrintAndBack()
    {
        window.print();
        history.go(-1);
    }



<input id="btnPrint" type="button" value="Print" onclick="PrintAndBack()" />


when user click on OK button on Print Dailog button Print is done and page is going back page.

But when user click on cancel button I want to stop this user on same page not going to back page.

Thanks in advance
Posted
Comments
ZurdoDev 23-Feb-12 7:52am
   
I haven't tried it before but I would see if window.print() gives a return value.
Rate this:
Please Sign up or sign in to vote.

Solution 1

Hi there,

In answer to your question (though I admit it is rather dissatisfying) qhat you wish to do is currently impossible. You cannot detect whether the cancel or print button was clicked after calling window.print(). Your only alternative is to do something like the following to ask the user if they wish to be redirected (or if they printed but asking that would be less transparent to the user...)

function PrintAndBack()
{
    window.print();
    if(confirm("Would you like to go back?"))
    {
        history.go(-1);
    }
    return false;
}

<input id="btnPrint" type="button" value="Print" onclick="return PrintAndBack();" />


Notice the extra return statements - these stop the page from refreshing after the button click if the user isn't being redirected. If the user is, then they make no difference.

Hope this helps,
Ed
   
v2
Rate this:
Please Sign up or sign in to vote.

Solution 2

If this is solely for IE you can use the onafterprint (warning - print preview will fire it as well as actually printing) event - outside of that, there is no way of knowing how it returned.

http://msdn.microsoft.com/en-us/library/windows/apps/hh441285.aspx[^]
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100