开发者

Why would window.print() and then window.close() called using register Page.ClientScript.RegisterStartupScript() method not work

I am trying to call the below method in Page_Load of an asp.net page:

Page.ClientScript.RegisterStartupScript(this.GetType(), "print", "<script language=\"javascript\" type=\"text/javascript\">window.print();window.close();</script>");

(or RegisterClientScriptBlock method).

This would give a message to close the page in IE (or even close without any message when called as a popup) but works well with Firefox. Surprisingly, the same events inside a function, when called onload of body will work well.

I can use workarounds to get this work but I was wondering why there is a 开发者_高级运维different behaviour here? Does anybody know?

This is the generated HTML:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>
 
</title></head>
<body>
    <form method="post" action="Default2.aspx" id="form1">
    <div>
        This is a test page!
    </div>
    
<script language="javascript" type="text/javascript">window.print();window.close();</script></form>
    
</body>
</html>


Try this:

<script language="javascript" type="text/javascript">
  window.print();
  window.onfocus = function () {
     window.close();
  }
</script>

From: Close window automatically after printing dialog closes

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜