·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> 网站建设开发 >> ASP.NET网站开发 >> 由window.history.back()引发的问题

由window.history.back()引发的问题

作者:佚名      ASP.NET网站开发编辑:admin      更新时间:2022-07-23

由window.history.back()引发的问题

由window.history.back()引发的问题

编写人:CC阿爸

2015-1-30

今天在这里,我想与大家一起分享由windows.history.back()引发的问题,笔者在实际开发当中遇到了多种不同的情况,在此做个小结,以供参考。有兴趣的同学,可以一同探讨与学习一下,否则就略过吧。

1.由于刚开始进行的web开发,很多时候使用的为html控件,因此处理起来相当简单

如以下两种方法:

window.history.back();(不会刷新)

window.location.href=’上一个页面.aspx’(会刷新)<inputtype=buttonvalue=刷新onclick="window.location.reload()"><inputtype=buttonvalue=前进onclick="window.history.go(1)"><inputtype=buttonvalue=后退onclick="window.history.go(-1)"><inputtype=buttonvalue=前进onclick="window.history.forward()"><inputtype=buttonvalue=后退onclick="window.history.back()">后退+刷新<inputtype=buttonvalue=后退onclick="window.history.go(-1);window.location.reload()">

2.但转到webcontrol时,发现这些就不能通用了。经过测试发现要采用如下操作(留意红色部分内容)

<asp:ButtonID="btnBack"Width="80px"runat="server"Text="Back"OnClientClick="window.history.back();returnfalse;">(不会刷新)

<asp:ButtonID="btnBack"Width="80px"runat="server"Text="Back"OnClientClick="window.location='P_PRQuoList.aspx';returnfalse;">(会刷新)<asp:ButtonID="btnBack"Width="80px"runat="server"Text="Back"OnClientClick="window.location.href=document.referrer;returnfalse;"></asp:Button>(会刷新)

3.C# Web程序中,如为页面按钮写返回上一页代码

this.RegisterClientScriptBlock("E","<scriptlanguage=javascript>history.go(-2);</script>");

其中,history.go(-2),要写为-2,因在按钮事件触发前,已刷新一次页面,所以应是-2。

Response.Write("<script language=Javascript>history.go(-2);</script>");

此处也要写为“-2”。跟直接写脚本的有所不同。history.back()是会上一页 i=1 history.go(i)去指定的某页

4.另:有时候,我们返回时,想保留上次输入的内容,又如何处理,一般情况下返回后,内容就被清空了。

网上同学讲.history.back(-1);是不能去掉的.. 用window.location.href='页面.aspx';

以下为通用的解决方案:

<HTML><HEAD>

<METAcontent=historyname=save><STYLE>.saveHistory{behavior:url(#default#savehistory);}</STYLE><METAcontent="MSHTML6.00.2800.1528"name=GENERATOR></HEAD><BODY>加了历史保留功能:<BR><TEXTAREAclass=saveHistory></TEXTAREA><BR>没有加的:<BR><TEXTAREA></TEXTAREA><P><INPUTonclick='javascript:location.href="http://www.google.com"'type=buttonvalue=点击进入,再按后退键试试?></P></BODY></HTML>

以下第二部分为我在程序中采用的解决方案

欢迎加入技术分享群

以下是其它网友总结的文章,写得很全面,现摘抄下来供大家参考

http://blog.csdn.net/durendong/article/details/9245863

打开一个jsp页面,肯定是用客户端脚本进行刷新了。Javascript刷新页面的几种方法:1history.go(0)2location.reload()3location=location4location.assign(location)5document.execCommand('Refresh')6window.navigate(location)7location.replace(location)

8document.URL=location.href