history.go(0) 不等于 location.href = location.href;

作者:vkvi 来源:ITPOW(原创) 日期:2020-5-20

history.go(0) 是刷新,location.href = location.href 也是刷新,二者似乎没有区别,然而在提交数据时还是有区别的。

浏览器 Chrome。

页面 ASP.NET(以 ASP.NET 为例,并不是说在 ASP.NET 中存在问题,在 PHP、JSP 中照样存在):一个 input(type=button),一个 TextBox,一个 Button。

程序

input 就是执行 history.go(0),Button 是将 TextBox 的值保存至数据库,并清空 TextBox。

演示

点击 Button 执行了保存,TextBox 也清空了,此时再点击 input,并没有任何提示,页面仅仅是闪烁一下,然后发现数据库中又多了一条刚才的信息,再点 input 又有一条……

解决

将 history.go(0) 换成 location.href = location.href,实现了页面刷新,数据库中没有多一条数据。

通过调试我们发现,history.go(0) 将之前提交的数据,在没有提示的情况下,又提交了一份,而 location.href = location.href 则不会。

相关文章