javascript - Change the get URL parameter on page refresh - Stack Overflow

I have a form in a popup. User fills the details and submits the form. On form submission, the popup is

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!

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!

Share Improve this question edited May 23, 2017 at 12:01 CommunityBot 11 silver badge asked Sep 30, 2014 at 7:26 SaravananSaravanan 6542 gold badges9 silver badges20 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 1

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

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;

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.

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);
    }
});
`$(window).unload(function() {
    var currentURL = document.location.href;
    var index = currentURL.indexOf("?error=");
    if(index > -1) 
        document.location.href = currentURL.substring(0, index); 
});`

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745401520a4626125.html

相关推荐

  • javascript - Change the get URL parameter on page refresh - Stack Overflow

    I have a form in a popup. User fills the details and submits the form. On form submission, the popup is

    3小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信