页面返回 history.go() 及 history.back()

在博客上看到有人这么说:
简单的说就是:go(-1):返回上一页,原页面表单中的内容会丢失;back():返回上一页,原页表表单中的内容会保留。
history.go(-1):后退+刷新history.back():后退
之所以注意到这个区别,是因为不同的浏览器后退行为也是有区别的,而区别就跟javascript:history.go()和history.back()的区别类似。
Chrome和ff浏览器后退页面,会刷新后退的页面,若有数据请求也会提交数据申请。类似于history.go(-1);
而safari(包括桌面版和ipad版)的后退按钮则不会刷新页面,也不会提交数据申请。类似于javascript:history.back();
但是,经测试:
window.history.go()/window.history.back()
只能实现页面返回上一页,但是并不刷新返回到的页面
所以,总结如下方法:
function goback(){
    window.location.reload() //刷新
    window.history.go(1) //前进
    window.history.go(-1) //后退(不刷新页面)
    window.history.forward() //前进
   window.history.back() 后退不刷新
}

返回并刷新页面,还是: window.location.href = document.referrer; // 返回上一页面并刷新

另外,提供一些其他返回刷新页面的方法

<a href="history.html" onclick="javascript:location.replace(this.href);event.returnValue=false; ">方法一:返回上一页并刷新页面</a> 
<a href="javascript:" onclick="self.location=document.referrer;">方法二:返回上一页并刷新</a> 
<a href="javascript:window.history.go(-1)">方法三:返回上一页,不刷新history.html</a>
<a href="javascript:document.parentWindow.location.reload()">方法四:返回上一页面刷新的是自己</a>
</body>
(有什么错误或者大家有什么建议,欢迎指出)


猜你喜欢

转载自blog.csdn.net/Freeky_ge/article/details/80730188