javascript:window.location.replace 与 window.location.reload() 刷新页面的不同效果

今天早上我发现一个问题,当一个网页的地址最后面是一个#时(比如:http://www.baidu.com/go.asp#),

执行:window.location.replace(window.location.href); 浏览器不刷新页面。

经过测试: window.location.href = window.location.href; 浏览器也不刷新页面。

经过测试:window.location.reload() ; 浏览器会刷新页面。

以前经过一些测试发现 window.location.replace(window.location.href); 比 window.location.reload() 节省了一些不必要的socket连接操作。

但我今天遇到了这个问题之后,代码简单的修改了一下,主要是加了一个对地址的判断:

function pageRefresh()

{

var url = window.location.href;
 var i = url.lastIndexOf("#");
 if(i > -1 && url.length == i + 1)
 {
  window.location.replace(url.substr(0,i)); //去掉#,这样浏览器才会刷新页面
 }
 else
 {
  window.location.replace(url);
 }

}

2011-03-16

原文地址:https://www.cnblogs.com/personnel/p/4583032.html