由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_3.在C# Web程序中,如為頁面按鈕寫返回上一頁代碼
this.RegisterClientScrjavascript>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.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
新聞熱點
疑難解答