我想使用以下命令重新加载页面:
window.location.reload(true);
但是我收到POSTDATA警告,因为刷新函数想要重新发送以前的POST表单数据。如何在没有此警告的情况下刷新页面?
更新:我无法控制项目!我无法解决POST本身!
只是在JavaScript中更改window.location
是危险的,因为用户仍然可以点击后退按钮并重新提交帖子,这可能会产生意外结果(例如重复购买)。 PRG是一个更好的解决方案
Use the Post/Redirect/Get (PRG) pattern
为了避免这个问题,许多Web应用程序使用PRG模式 - 而不是直接返回HTML页面,POST操作返回重定向命令(使用HTTP 303响应代码(有时是302)和HTTP“Location”响应头),指示浏览器使用HTTP GET请求加载不同的页面。然后可以安全地将结果页面加入书签或重新加载,而不会产生意外的副作用。
<html:form name="Form" type="abc" action="abc.do" method="get" onsubmit="return false;">
method="get"
- 解决问题。
if method="post"
然后只有警告来了。
我已经编写了一个函数,可以在没有提交帖子的情况下重新加载页面,它也可以用于哈希。
我通过在名为reload
的URL中添加/修改GET参数来实现此目的,方法是使用当前时间戳(以ms为单位)更新其值。
var reload = function () {
var regex = new RegExp("([?;&])reload[^&;]*[;&]?");
var query = window.location.href.split('#')[0].replace(regex, "$1").replace(/&$/, '');
window.location.href =
(window.location.href.indexOf('?') < 0 ? "?" : query + (query.slice(-1) != "?" ? "&" : ""))
+ "reload=" + new Date().getTime() + window.location.hash;
};
请记住,如果要在href属性中触发此函数,请按以下方式实现:href="javascript:reload();void 0;"
使其成功运行。
我的解决方案的缺点是它将更改URL,因此这个“重新加载”不是真正的重新加载,而是一个具有不同查询的加载。不过,它可以满足您的需求,就像它对我一样。
你没有被迫使用javascript来做所有事情。许多问题都有像这样的简单解决方案。你可以使用这个棘手的锚:
<a href=".">Continue</a>
当然我知道你可能需要一个自动解决方案,但这在很多情况下会有所帮助。
祝好运
在html中使用元刷新
<meta http-equiv='refresh' content='1'>
在php中使用元刷新
echo "<meta http-equiv='refresh' content='1'>"
这是一个应始终有效且不会删除哈希的解决方案。
let currentPage = new URL(window.location.href);
currentPage.searchParams.set('r', (+new Date * Math.random()).toString(36).substring(0, 5));
window.location.href = currentPage.href;
如果你使用GET
方法而不是POST
那么我们不能使用表单字段值。如果你使用window.opener.location.href = window.opener.location.href;
然后我们可以触发数据库,我们可以获得值,但唯一的事情是JSP
不刷新,尽管scriplet具有形式值。
没有警告就无法刷新; refresh指示浏览器重复上一个操作。如果重复上一个操作涉及重新提交数据,则由浏览器选择是否警告用户。
您可以通过以下操作重新导航到具有新会话的同一页面:
window.location = window.location.href;
我有一些问题,锚/ hash-urls(包括#)没有使用Rex的解决方案重新加载...
所以我最终删除了哈希部分:
window.location = window.location.href.split("#")[0];
要绕过POST警告,您必须使用完整URL重新加载页面。工作良好。
window.location.href = window.location.protocol +'//'+ window.location.host + window.location.pathname;
window.location.replace(window.location.href)怎么样;
你可以使用JavaScript:
window.location.assign(document.URL);
在Firefox和Chrome上为我工作
检查此链接:http://www.codeproject.com/Tips/433399/PRG-Pattern-Post-Redirect-Get
如果您正处于完成发布数据并且只想重新查看页面的阶段,您可以使用window.location甚至可以附加一个随机字符串作为查询参数来保证新版本的页。
Nikl的版本没有传递获取查询参数,我使用了以下修改版本:
window.location.href = window.location.protocol +'//'+ window.location.host + window.location.pathname + window.location.search;
或者在我的情况下,我需要刷新最顶层的页面\框架,所以我使用了以下版本
window.top.location.href = window.top.location.protocol +'//'+ window.top.location.host + window.top.location.pathname + window.top.location.search;
这很有效
<button onclick="window.location.href=window.location.href; return false;">Continue</button>
没有这个原因它没有用的原因
return false;is that previously it treated that as a form submit button. With an explicit return false on it, it doesn't do the form submit and just does the reload of the same page that was a result of a previous POST to that page.