window.location =和window.location.replace()有什么区别? [英] What's the difference between window.location= and window.location.replace()?

查看:541
本文介绍了window.location =和window.location.replace()有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这两行之间有区别吗?

var url = "http://www.google.com/";
window.location = url;
window.location.replace(url);


推荐答案

window.location 在您的历史记录中添加一项项目,您可以(或应该)点击返回并返回当前页面。

window.location adds an item to your history in that you can (or should be able to) click "Back" and go back to the current page.

window.location.replace 替换当前的历史记录项,因此您无法返回。

window.location.replace replaces the current history item so you can't go back to it.

参见 window.location


assign(url):将文档加载到提供的
URL。

assign(url): Load the document at the provided URL.

replace(url):将当前的
文档替换为提供的
网址。与
assign()方法的区别在于使用
replace()后当前页面不会在会话历史记录中保存
,这意味着
用户将无法使用Back
按钮导航到该页面。

replace(url):Replace the current document with the one at the provided URL. The difference from the assign() method is that after using replace() the current page will not be saved in session history, meaning the user won't be able to use the Back button to navigate to it.

哦,一般来说:

window.location.href = url;

优惠:

window.location = url;

这篇关于window.location =和window.location.replace()有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆