How to reload a page using JavaScript

前端 未结 19 2587
隐瞒了意图╮
隐瞒了意图╮ 2020-11-22 00:46

How can I reload the page using JavaScript?

I need a method that works in all browsers.

相关标签:
19条回答
  • 2020-11-22 01:42

    Use this button to refresh the page

    DEMO

    <input type="button" value="Reload Page" onClick="document.location.reload(true)">
    
    0 讨论(0)
  • 2020-11-22 01:43

    Here are 535 ways to reload the page using JavaScript, the easiest being location = location.

    These are the first 50:

    location = location
    location = location.href
    location = window.location
    location = self.location
    location = window.location.href
    location = self.location.href
    location = location['href']
    location = window['location']
    location = window['location'].href
    location = window['location']['href']
    location = window.location['href']
    location = self['location']
    location = self['location'].href
    location = self['location']['href']
    location = self.location['href']
    location.assign(location)
    location.replace(location)
    window.location.assign(location)
    window.location.replace(location)
    self.location.assign(location)
    self.location.replace(location)
    location['assign'](location)
    location['replace'](location)
    window.location['assign'](location)
    window.location['replace'](location)
    window['location'].assign(location)
    window['location'].replace(location)
    window['location']['assign'](location)
    window['location']['replace'](location)
    self.location['assign'](location)
    self.location['replace'](location)
    self['location'].assign(location)
    self['location'].replace(location)
    self['location']['assign'](location)
    self['location']['replace'](location)
    location.href = location
    location.href = location.href
    location.href = window.location
    location.href = self.location
    location.href = window.location.href
    location.href = self.location.href
    location.href = location['href']
    location.href = window['location']
    location.href = window['location'].href
    location.href = window['location']['href']
    location.href = window.location['href']
    location.href = self['location']
    location.href = self['location'].href
    location.href = self['location']['href']
    location.href = self.location['href']
    ...
    
    0 讨论(0)
  • 2020-11-22 01:44

    I was looking for some information regarding reloads on pages retrieved with POST requests, such as after submitting a method="post" form.

    To reload the page keeping the POST data, use:

    window.location.reload();

    To reload the page discarding the POST data (perform a GET request), use:

    window.location.href = window.location.href;

    Hopefully this can help others looking for the same information.

    0 讨论(0)
  • 2020-11-22 01:47

    This should work:

    window.location.href = window.location.href.split( '#' )[0];
    

    or

    var x = window.location.href;
    x = x.split( '#' );
    window.location.href = x[0];
    

    I prefer this for the following reasons:

    • Removes the part after the #, ensuring the page reloads on browsers that won't reload content that has it.
    • It doesn't ask you if want to repost last content if you recently submit a form.
    • It should work even on most recent browsers. Tested on Lasted Firefox and Chrome.

    Alternatively, you may use the most recent official method for this task

    window.location.reload()
    
    0 讨论(0)
  • 2020-11-22 01:48

    Thank you, this post was very helpful, not only to reload the page with the suggested answer, but also as well to give me the idea to place a jQuery UI icon to a button:

    <button style="display:block; vertical-align:middle; height:2.82em;"
            title="Cargar nuevamente el código fuente sin darle un [Enter] a la dirección en la barra de direcciones"
            class="ui-state-active ui-corner-all ui-priority-primary" 
            onclick="javascript:window.location.reload(true);">
        <span style="display:inline-block;" class="ui-icon ui-icon-refresh"></span>
        &nbsp;[<b>CARGAR NUEVAMENTE</b>]&nbsp;
    </button>
    

    2016-07-02

    0 讨论(0)
  • 2020-11-22 01:49

    The reload() method is used to reload the current document.

    The reload() method does the same as the reload button in your browser.

    By default, the reload() method reloads the page from the cache, but you can force it to reload the page from the server by setting the forceGet parameter to true: location.reload(true).

            location.reload();
    
    0 讨论(0)
提交回复
热议问题