[英]How to go back to previous page using Javascript in ASP.NET
I have the following scenario: 我有以下场景:
I have the following script in my page: 我的页面中有以下脚本:
function GoBackToPreviousPage () {
parent.history.back();
return false;
}
In Code Behind I have the following 在Code Behind中我有以下内容
If (some conditions are met)
Page.ClientScript.RegisterStartupScript(this.GetType(), "GoBackToPreviousPage", "GoBackToPreviousPage();", true);
If the conditions are met I want the page to go back to the previous page, the reason I'm doing this in javascript instead of on the server is because (parent.history.back();) maintains a history of all the previous pages 如果满足条件,我希望页面返回到上一页,我在javascript中而不是在服务器上执行此操作的原因是因为(parent.history.back();)维护了以前的所有历史记录网页
I noticed that the after the RegisterStartupScript is executed and postback happens the script is executed but it stays on the same page, it does not go back to the previous page 我注意到在执行RegisterStartupScript之后发生了回发,脚本被执行但是它停留在同一页面上,它不会回到上一页
The problem is caused by the PostBack that occurs before the execution of the javascript function. 问题是由执行javascript函数之前发生的PostBack引起的。 You click the button and the current page is saved in the browser history. 单击该按钮,当前页面将保存在浏览器历史记录中。 So, when the page reloads and the script executes, you are in the same page. 因此,当页面重新加载并执行脚本时,您将处于同一页面中。 So, a not-so-pretty solution is to go back two pages in history: 因此,一个不那么漂亮的解决方案是回溯历史中的两个页面:
function GoBackToPreviousPage() {
window.history.go(-2);
return false;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.