Javascript 如何在没有浏览器询问是否要重新发送的情况下刷新网页
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6661175/
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
How can I refresh a webpage without the browser asking if I want to resend again
提问by code511788465541441
I got the following code to refresh my webpage. It works great if I don't submit anything with POST but if i do, i get a message from the browser when my webpage refreshes (see image below)
我得到了以下代码来刷新我的网页。如果我不使用 POST 提交任何内容,它会很好用,但如果我这样做了,当我的网页刷新时,我会收到来自浏览器的消息(见下图)
location.reload(true);
I'm not looking for the browser settings tweak. I'm looking for alternative code to refresh without asking.
我不是在寻找浏览器设置调整。我正在寻找无需询问即可刷新的替代代码。
回答by detaylor
This is caused due to the page being requested by POST
instead of GET
. Refreshing will resubmit the POST
data. You can force a get using window.location = window.location.href;
.
这是由于请求的页面POST
而不是GET
。刷新将重新提交POST
数据。您可以强制使用window.location = window.location.href;
.
If you want to allow people to reload the page through their browser controls then you will need to implement the PRG patternwhich redirects to a GET
after a POST
.
如果你想允许人们通过他们的浏览器控件重新加载页面,那么你需要实现PRG 模式,它重定向到 aGET
之后POST
。
回答by dpp
This will request for a page and not a reload.
这将请求一个页面而不是重新加载。
window.location = window.location;
回答by spraff
The solution: don't POST. GET is supposed to be idempotent (up to environmental changes), POST is supposed to be a modification.
解决方案:不要发布。GET 应该是幂等的(取决于环境变化),POST 应该是一种修改。
After the POST, you should be performing an immediate GET to reload the page with the result of the POST. Then you can refresh at will.
POST 后,您应该立即执行 GET 以使用 POST 结果重新加载页面。然后就可以随意刷新了。
Example:
例子:
page_1.html
page_1.html
<form method="POST" action="go.php"> ... </form>
go.php:
去.php:
<?php
// do stuff...
header('Location: http://www.foo.com/page2.html');
?>
page2.html:
page2.html:
<script type="text/javascript"> location .reload (true); </script>
回答by Gats
This is controlled on the browser end and not yours (ie you CAN'T control it). If you want to show the user a page that can be refreshed without the repost option, you'll have to do it via a redirect following the post to a page that you retrieve with a GET not a POST.
这是在浏览器端控制的,而不是你的(即你不能控制它)。如果您想向用户显示一个无需重新发布选项即可刷新的页面,则必须通过在帖子后重定向到您使用 GET 而不是 POST 检索的页面来完成。
Try a javascript redirect. Or alternatively, POST the information using AJAX which will mean refresh reloads the original page, not the results of your ajax post.
尝试 javascript 重定向。或者,使用 AJAX 发布信息,这意味着刷新会重新加载原始页面,而不是 ajax 发布的结果。
回答by TheBrain
you can save your data to window.name and use the window.location = window.location.href trick. and when onload, check if you have data in window.name, get it back and remove it from there. i use the window.name as "session" storage quite often for situations like this.
您可以将数据保存到 window.name 并使用 window.location = window.location.href 技巧。并在加载时,检查 window.name 中是否有数据,将其取回并从那里删除。对于这种情况,我经常使用 window.name 作为“会话”存储。
回答by Sergio Bustamante
(I'm spanish speaker, I′ll do my best) In my case, I have a button to looking for open tickets in a DB, so, i need to press that button every time that I wanna know if there are new ones, I resolved with the next javascript function
(我会说西班牙语,我会尽力而为)就我而言,我有一个按钮可以在数据库中查找未结票,因此,每次我想知道是否有新票时,我都需要按下该按钮, 我用下一个 javascript 函数解决了
<script type="text/javascript" language="javascript">
setTimeout('document.getElementById("btn_busca_ticket").click()', 10000);
</script>
where 10000 is the time between every refresh, (10 seconds)
其中 10000 是每次刷新之间的时间(10 秒)
if you don't have a button, you can create a hidden one with an ID and a submit (it's not so elegant, but works for me)
如果你没有按钮,你可以创建一个带有 ID 和提交的隐藏按钮(它不是那么优雅,但对我有用)