Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
251 views
in Technique[技术] by (71.8m points)

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

Is there a difference between these two lines?

(这两条线之间有区别吗?)

var url = "http://www.google.com/";
window.location = url;
window.location.replace(url);
  ask by Aaron Digulla translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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将一个项目添加到您的历史记录中,您可以(或应该能够)单击“返回”并返回当前页面。)

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

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

See window.location :

(请参阅window.location :)

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

(assign(url) :在提供的URL处加载文档。)

replace(url) :Replace the current document with the one at the provided URL.

(replace(url) :将当前文档替换为提供的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.

(与assign()方法的不同之处在于,在使用replace() ,当前页面将不会保存在会话历史记录中,这意味着用户将无法使用“后退”按钮导航到该页面。)

Oh and generally speaking:

(哦,一般来说:)

window.location.href = url;

is favoured over:

(受到青睐:)

window.location = url;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...