[英]Change the get URL parameter on page refresh
我在弹出窗口中有一个表单。 用户填写详细信息并提交表单。 在表单提交时,弹出窗口关闭,用户被重定向到主页。 提交时,主屏幕上会显示一条消息(表单提交是否成功)。 这是通过在主页url中添加参数来完成的。
当用户打开网站时,网址将为..../home
。 用户点击弹出按钮并提交表单。
成功后,网址将被重定向到..../home?error=0
在失败时,网址将被重定向到..../home?error=1
根据错误标志值,我在主页上显示消息。
在此步骤之后,如果用户刷新主页,他仍然会收到表单提交消息,因为错误标志存在于URL中。 但刷新后,我不想在主页上显示该消息。
因此,我尝试使用以下代码删除/设置页面刷新时的错误标志。
> $(window).unload(function() {
> var currentURL = window.location.href;
> var index = currentURL.indexOf("?error=");
> if(index > -1) {
> window.location.href = currentURL.substring(0, index);
> } });
它适用于Firefox,但不适用于Chrome。 未在其他浏览器中测试过。 但是jQuery unload已经在1.8中被弃用了。
如何在页面刷新时删除/设置错误标志? 或者是否有其他方法在页面刷新后不在主页上显示消息。 该解决方案应该适用于所有主流浏览器(IE,Chrome,firefox。歌剧,safari)
注意:这是在页面刷新时更改URL的延续
谢谢你的帮助!
您可以使用$ _SESSION,在这种情况下,在显示页面后清除POST值。 下次加载页面时,$ _SESSION变量将消失。 按顺序:
表单提交>方法= $ _SESSION
加载主页,检查$ _SESSION变量集是否显示消息
清除$ _SESSION变量
重新加载页面? 未设置post变量,因此转到主页而不显示消息
我会在服务器端而不是客户端解决此问题,例如使用$ _SESSION标志。
在家里的代码中把这段代码放在最开头(如果你使用的是PHP):
<?php
// If there is an error message to show
if (isset($_GET['error'])){
// and we still haven't shown it
if (empty($_SESSION['error_shown'])){
$_SESSION['error_shown'] = 1; // set the flag that the message is showing right now
}else{
// if we already have shown the error message, unset the flag and redirect to the URL with no message
unset($_SESSION['error_shown']);
header('Location: /home');
die();
}
}
?>
... your error messages here with the rest of content
代码可能有一些错误,我还没有测试过,但这个想法很清楚。
我面临的同样问题和解决方案下面的问题帮助我试试这个。
阅读网址:
var url = window.location;
改变它:
window.location.replace(url);
改变和重定向:
window.location = url;
beforeunload应该可以在Chrome上正常运行!
$(window).on('beforeunload', function() {
var currentURL = window.location.href;
var index = currentURL.indexOf("?error=");
if(index > -1) {
window.location.href = currentURL.substring(0, index);
}
});
`$(window).unload(function() {
var currentURL = document.location.href;
var index = currentURL.indexOf("?error=");
if(index > -1)
document.location.href = currentURL.substring(0, index);
});`
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.