js跳转到上一页面失败的解决方法

发布时间:2020-01-24编辑:脚本学堂
有二个页面a.html和b.aspx,当从a.html跳到b.aspx后不做任何让页面回传的操作,用history.go(-1)可以回到a.html页面,如果使用Click或Change事件等激发了页面的回传,此时用history.go(-1)就不能回到a.html页面了,真是让人头疼。

解决方法:
只能想办法记录到页面回传的次数N了,然后使用history.go(-n),便可以回到a.html页面了。
这里强高一下history.go(-n),可以灵活控制跳转回页面的次数哦。

在b.aspx页面中放一个控件记录其回传的次数,初始值为1。
 

复制代码 代码示例:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
<!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 runat="server">
<title></title>
<script>
function goto() {
var n=document.getElementById("TextBox1").value;
var n=Number(n);
history.go(-n);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
<input id="Reset1" type="button" value="reset" onclick="goto()"/>
<asp:TextBox ID="TextBox1" runat="server" ToolTip="放个控件保存页面回传次数">1</asp:TextBox>
</div>
</form>
</body>
</html>

页面b.aspx代码:
 

复制代码 代码示例:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.TextBox1.Text = "1";
}
else
{
this.TextBox1.Text = Convert.ToString(Convert.ToInt16(this.TextBox1.Text) + 1);
}
}
 

这样,无论怎样操作,点返回时都可以返回a.html了。