Javascript 如何在打印文档后*重定向到另一个页面?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7826209/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 03:45:10  来源:igfitidea点击:

How to I redirect to another page *after* printing document?

javascriptasp.net

提问by Chidi Okeh

I have an event called SubmitResponse().

我有一个名为SubmitResponse().

A user is presented with a list of questions and possible responses. After completing the responses, the user clicks the Preview button. If responses are correct, the user clicks the SubmitResponse button and then SubmitResponse()processes it.

向用户呈现问题列表和可能的响应。完成响应后,用户单击预览按钮。如果响应正确,则用户单击 SubmitResponse 按钮​​,然后对其进行SubmitResponse()处理。

Upon clicking the SubmitResponse button, a print screen pops up for the user to print a copy. This is the calling code for the JS print feature.

单击 SubmitResponse 按钮​​后,会弹出打印屏幕供用户打印副本。这是JS打印功能的调用代码。

finsub.Attributes.Add("OnClick", "print_form()")

Works fine but there is one problem. We would like the user to be redirected to the screen that displays results of his/her response with the code below.

工作正常,但有一个问题。我们希望用户被重定向到显示他/她响应结果的屏幕,代码如下。

Response.Redirect("results.aspx")

What is happening is that once the user clicks the submitResponses button, s/he is immediately redirected to the results.aspx pageand the print feature is no longer available.

发生的情况是,一旦用户单击 submitResponses 按钮,他/她会立即被重定向到results.aspx page并且打印功能不再可用。

Is there a way to work around this?

有没有办法解决这个问题?

回答by Kelsey

You can do the printing and redirect all via javascript. Here is an example that should work:

您可以通过javascript进行打印和重定向。这是一个应该有效的示例:

function doPrint() {
    window.print();            
    document.location.href = "Somewhere.aspx"; 
}

Link it to a Button:

将其链接到Button

<asp:Button ID="btnPrint" runat="server" Text="Print"
    OnClientClick="doPrint(); return false;" />

回答by SyntaxGoonoo

For cross browser compatibility, you need to introduce a delay between the window.print() call and the redirect.

为了跨浏览器兼容性,您需要在 window.print() 调用和重定向之间引入延迟。

I find the following works well (using JQuery framework to print a page as soon as it loaded):

我发现以下效果很好(使用 JQuery 框架在页面加载后立即打印):

 <script type="text/javascript">
    $(document).ready(function () {
        window.print();
        setTimeout("closePrintView()", 3000);
    });
    function closePrintView() {
        document.location.href = 'somewhere.html';
    }
</script>

This can be easily adapted for "Print" buttons and links etc.

这可以很容易地适用于“打印”按钮和链接等。

回答by reza baghiee

you can use this code :

您可以使用此代码:

window.print();
window.onafterprint = function(event) {
    window.location.href = 'index.php'
};

using window.print() callBack!!

使用 window.print() 回调!!

回答by Emmanuel N

Redirect on client side using javascript/jQuery

使用 javascript/jQuery 在客户端重定向

  onclick="window.print();window.location.href='results.aspx';"

For your case

对于您的情况

 insub.Attributes.Add("OnClick", "print_form();window.location.href='results.aspx';")

回答by user973254

<script type="text/javascript">window.location = 'url.aspx';</script>