location中href和replace的区别

1.有两种方式:

a、使用 location.href:window.location.href=“success.html”;

b、使用location.replace:window.location.replace(“new_file.html”);

2.区别是什么?

结果:href相当于打开一个新页面,replace相当于替换当前页面 下面是说明:

a.当第一个页面idnex.html跳转到 success.html 时 href是 跳转一个新的页面

<body>
		<h1>index.html</h1>
		<a href="javascript:void(0)" onclick="load()"> 点击连接</a>
		<script>
			function load() {
				alert("跳转第一次");
				/* href是跳转一个新的页面 */
				window.location.href = "success.html";
			}
		</script>
</body>

b.当第二次跳转的时候window.location.replace();

是替换当前页 将success.html换成了new_file.html而不是在当前页面新打开了一个出窗口

<body>
		<h1>succeess</h1>
		<a href="javascript:void(0)" onclick="load()"> 点击连接</a>
		<script>
			function load(){
				alert("跳转第二次");
				/* 替换当前页 */
				window.location.replace("new_file.html");
			}
		</script>
	</body>

第三个页面:返回的时候相当于页面时 (success.html==new_file.html) , 按浏览器后退按钮将返回的是上第一个页面 index.html。

而如果将success.html中跳转 replace 换成 , location.href =“new_file.html”, 那么这个时候测试 返回页面是逐一返回页面 返回的是success.html。

<body>
		<h1> new_file.html</h1>
		当前页==相当于这个页面是success.html;
</body>

微信公众号内window.location.href无法跳转页面 

方案一 添加时间戳


var url = "xxx.html";
window.location.href=url+'?timestamp='+((new Date()).getTime()+Math.random()); 

方案二  window.open

使用

var url = "xxx.html"; 
window.open(url, '_blank');

猜你喜欢

转载自blog.csdn.net/qq_63358859/article/details/143336369