Reload browser window after POST without prompting user to resend POST data

后端 未结 14 2022
盖世英雄少女心
盖世英雄少女心 2020-11-29 05:47

When a user visits my website there is a \"Login\" link on every page. Clicking this uses some JavaScript to show an overlay window where the user is prompted for their cred

相关标签:
14条回答
  • 2020-11-29 06:04

    Use

    RefreshForm.submit(); 
    

    instead of

    document.location.reload(true); 
    
    0 讨论(0)
  • 2020-11-29 06:05

    I had the same problem as you.

    Here's what I did (dynamically generated GET form with action set to location.href, hidden input with fresh value), and it seems to work in all browsers:

    var elForm=document.createElement("form");
    elForm.setAttribute("method", "get");
    elForm.setAttribute("action", window.location.href);
    
    var elInputHidden=document.createElement("input");
    elInputHidden.setAttribute("type", "hidden");
    elInputHidden.setAttribute("name", "r");
    elInputHidden.setAttribute("value", new Date().getTime());
    elForm.appendChild(elInputHidden);
    
    if(window.location.href.indexOf("?")>=0)
    {
        var _arrNameValue;
        var strRequestVars=window.location.href.substr(window.location.href.indexOf("?")+1);
        var _arrRequestVariablePairs=strRequestVars.split("&");
        for(var i=0; i<_arrRequestVariablePairs.length; i++)
        {
            _arrNameValue=_arrRequestVariablePairs[i].split("=");
    
            elInputHidden=document.createElement("input");
            elInputHidden.setAttribute("type", "hidden");
            elInputHidden.setAttribute("name", decodeURIComponent(_arrNameValue.shift()));
            elInputHidden.setAttribute("value", decodeURIComponent(_arrNameValue.join("=")));
            elForm.appendChild(elInputHidden);
        }
    }
    
    document.body.appendChild(elForm);
    elForm.submit();
    
    0 讨论(0)
  • 2020-11-29 06:06

    This worked

    <button onclick="window.location.href=window.location.href; return false;">Continue</button>
    

    The reason it doesn't work without the return false; is that the button click would trigger a form submit. 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.

    0 讨论(0)
  • 2020-11-29 06:10

    This works too:

    window.location.href = window.location.pathname + window.location.search
    
    0 讨论(0)
  • 2020-11-29 06:11
    window.location.href = window.location.href;
    

    Don't ask my why it works..but it does :). Just the way the js engine interprets the logic I suppose.

    0 讨论(0)
  • 2020-11-29 06:11

    If you have hashes '#' in your URL, all the solutions here do not work. This is the only solution that worked for me.

    var hrefa = window.location.href.split("#")[1];
    var hrefb = window.location.href.split("#")[2];
    window.location.href  = window.location.pathname +  window.location.search + '&x=x#' + hrefa + '#' + hrefb;
    

    The URL has to be different for it to reload if you have a hash. The &x=x does that here. Just substitute this for something you can ignore. THis is abit of a hack, unable to find a better solution..

    Tested in Firefox.

    0 讨论(0)
提交回复
热议问题