转到上一页和刷新 - 使用 javascript/Jquery 在一个按钮中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8695451/
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
Go To Previous Page and Refresh - both in one button using javascript/Jquery
提问by black_belt
is there any way to create a button which will on click take you to previous page and then refresh the page immediately? I use the following code to go back to previous page but it does not reload/refresh..
有什么方法可以创建一个按钮,点击后会带你到上一页,然后立即刷新页面?我使用以下代码返回上一页,但它不会重新加载/刷新..
<input type="button" value="Back" onClick="javascript: history.go(-1)">
Thanks :)
谢谢 :)
回答by Jesse Bunch
The only way to refresh the loaded page would be to use window.location.reload()
in the new page.
刷新加载页面的唯一方法是window.location.reload()
在新页面中使用。
You could try getting the last URL using document.referrer
(or by keeping a history of their page views in a cookie) and then adding something to the hash to make it like:
您可以尝试使用document.referrer
(或通过在 cookie 中保留其页面浏览历史记录)获取最后一个 URL ,然后向哈希添加一些内容以使其如下所示:
http://stackoverflow.com#didGoBack
http://stackoverflow.com#didGoBack
Then, in javascript, check to see if that hash exists using window.location.hash
and if so, reload the page. Note. the referrer won't be set unless they arrived at that page via a link.
然后,在 javascript 中,检查该哈希是否存在window.location.hash
,如果存在,则重新加载页面。笔记。除非他们通过链接到达该页面,否则不会设置引用者。
回答by Yucel Daglar
I think I invented a way of doing this. By adding a random parameter to url, we force browser to refresh...
我想我发明了一种方法来做到这一点。通过向 url 添加随机参数,我们强制浏览器刷新...
var backLocation = document.referrer;
if (backLocation) {
if (backLocation.indexOf("?") > -1) {
backLocation += "&randomParam=" + new Date().getTime();
} else {
backLocation += "?randomParam=" + new Date().getTime();
}
window.location.assign(backLocation);
}