Reputation: 654
I have a form in a popup. User fills the details and submits the form. On form submission, the popup is closed and the user is redirected to the homepage. On submission, a message is displayed on the home screen (whether form submission is success or not). This is done by adding a parameter in the home page url.
When user opens the site, the url will be ..../home
. And the user clicks on the popup button and submitting a form.
On success, the url will be redirected to ..../home?error=0
On Failure, the url will be redirected to ..../home?error=1
Based on the error flag value, i display the message on home page.
After this step, if user refreshes the home page, he still gets the form submission message because the error flag is present in the url. But after refresh, i dont want to display the message on home page.
So, i tried to remove/set the error flag on page refresh using the following code.
> $(window).unload(function() {
> var currentURL = window.location.href;
> var index = currentURL.indexOf("?error=");
> if(index > -1) {
> window.location.href = currentURL.substring(0, index);
> } });
It works well in Firefox but not in chrome. Not tested in other browsers. But jQuery unload has been deprecated in 1.8.
How to remove/set the error flag on page refresh? Or is there any other method for not to display the message on home page after page refresh. The solution should work in all major browsers (IE, chrome, firefox. opera, safari)
Note: This is a continuation of Change the URL on page refresh
Thanks for your help!
Upvotes: 1
Views: 6735
Reputation: 1082
I would solve this problem on server side instead of client side, for example using $_SESSION flag.
In the code of home put this code at the very beginning(if you are using 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
The code could has some errors, I haven't tested it, but the idea is clear.
Upvotes: 1
Reputation: 368
the same problem i've faced and below solution helped me out try this.
to read the url:
var url = window.location;
to change it:
window.location.replace(url);
to change and redirect:
window.location = url;
Upvotes: 1
Reputation: 1
`$(window).unload(function() {
var currentURL = document.location.href;
var index = currentURL.indexOf("?error=");
if(index > -1)
document.location.href = currentURL.substring(0, index);
});`
Upvotes: 0
Reputation: 425
You could use a $_SESSION, in that case clear the POST values after displaying the page. The next time the page is loaded the $_SESSION variable will be gone. In sequence:
Form submit > method = $_SESSION
Load home page, check if $_SESSION variable set > display the message
Clear the $_SESSION variable
Reload the page? The post variable is not set so go to the home page without displaying the message
Upvotes: 1
Reputation: 3349
beforeunload should work fine on 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);
}
});
Upvotes: 0