Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

javascript - window.location.href = window.location.href和window.location.reload()之间的区别(Difference between window.location.href=window.location.href and window.location.reload())

What is the difference between JavaScript's

(JavaScript的有什么区别)

window.location.href = window.location.href

and

(和)

window.location.reload()

functions?

(功能?)

  ask by Brian translate from so

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

1 Answer

0 votes
by (71.8m points)

If I remember correctly, window.location.reload() reloads the current page with POST data, while window.location.href=window.location.href does not include the POST data.

(如果我没记错的话, window.location.reload()用POST数据重新加载当前页面,而window.location.href=window.location.href不包含POST数据。)

As noted by @W3Max in the comments below, window.location.href=window.location.href will not reload the page if there's an anchor (#) in the URL - You must use window.location.reload() in this case.

(如@ W3Max在下面的注释中所指出的,如果URL中有锚点(#),则window.location.href=window.location.href将不会重新加载页面-在这种情况下,您必须使用window.location.reload() 。)

Also, as noted by @Mic below, window.location.reload() takes an additional argument skipCache so that with using window.location.reload(true) the browser will skip the cache and reload the page from the server.

(另外,如下面的@Mic所述, window.location.reload()带有附加参数skipCache以便浏览器使用window.location.reload(true)将跳过缓存并从服务器重新加载页面。)

window.location.reload(false) will do the opposite, and load the page from cache if possible.

(window.location.reload(false)将执行相反的操作,并在可能的情况下从缓存加载页面。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...