javascript 使用 JS 刷新浏览器窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19791148/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 16:48:36  来源:igfitidea点击:

Refresh browser window using JS

javascript

提问by Kamran Ahmed

I am trying to refresh the browser window using the following code:

我正在尝试使用以下代码刷新浏览器窗口:

window.location = window.location.href;

but it doesn't refresh the window, however when I try to do this:

但它不会刷新窗口,但是当我尝试这样做时:

window.location = "http://www.google.com";

It does redirect me to Google. Can anyone please tell me what I am doing wrong here? Why doesn't it refresh the browser window?

它确实将我重定向到 Google。谁能告诉我我在这里做错了什么?为什么不刷新浏览器窗口?

回答by MildlySerious

Use window.location.reload(), it does exactly that.

使用window.location.reload(),它正是这样做的。

回答by Fokwa Best

location.reload(forceGet)can do the trick. Also, you can set the parameter forceGet to trueso that it reloads the page from the server and not from the browser cache. Doing this solve me some issues with IE11 and Edge. By default (location.reload()) will reload from the cache.

location.reload(forceGet)可以做到这一点。此外,您可以将参数 forceGet 设置为,true以便它从服务器而不是浏览器缓存重新加载页面。这样做可以解决 IE11 和 Edge 的一些问题。默认情况下 ( location.reload()) 将从缓存中重新加载。

Read: http://www.w3schools.com/jsref/met_loc_reload.asp

阅读:http: //www.w3schools.com/jsref/met_loc_reload.asp

In the past I did something like window.location = ''to refresh the current page but faced issues (wasn't getting the complete url) with Edge and IE 11 even though it worked well on all other major browsers. So I eventually used location.reload(true)to get to work on Edge.

过去,我做了一些类似window.location = ''刷新当前页面的事情,但在 Edge 和 IE 11 上遇到了问题(没有获得完整的 url),尽管它在所有其他主要浏览器上运行良好。所以我最终习惯于location.reload(true)在 Edge 上工作。

回答by vinay Maneti

you can even use location.reload();

你甚至可以使用 location.reload();

回答by MSMOHAN

In my experience the following is the best one

根据我的经验,以下是最好的

window.location.reload()

Also we can make it like,

我们也可以让它像,

window.location.href = window.location.href

the second one is getting the current location and assigning the location as current location :)

第二个是获取当前位置并将该位置指定为当前位置:)