How to reload a page using JavaScript

前端 未结 19 2502
隐瞒了意图╮
隐瞒了意图╮ 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:24

    You can simply use

    window.location=document.URL
    

    where document.URL gets the current page URL and window.location reloads it.

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

    JavaScript 1.0

    window.location.href = window.location.pathname + window.location.search + window.location.hash;
    // creates a history entry
    

    JavaScript 1.1

    window.location.replace(window.location.pathname + window.location.search + window.location.hash);
    // does not create a history entry
    

    JavaScript 1.2

    window.location.reload(false); 
    // If we needed to pull the document from
    //  the web-server again (such as where the document contents
    //  change dynamically) we would pass the argument as 'true'.
    
    0 讨论(0)
  • 2020-11-22 01:27

    If you put

    window.location.reload(true);
    

    at the beginning of your page with no other condition qualifying why that code runs, the page will load and then continue to reload itself until you close your browser.

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

    To make it easy and simple, use location.reload(). You can also use location.reload(true) if you want to grab something from the server.

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

    Automatic reload page after 20 seconds.

    <script>
        window.onload = function() {
            setTimeout(function () {
                location.reload()
            }, 20000);
         };
    </script>
    
    0 讨论(0)
  • 2020-11-22 01:30

    You can perform this task using window.location.reload();. As there are many ways to do this but I think it is the appropriate way to reload the same document with JavaScript. Here is the explanation

    JavaScript window.location object can be used

    • to get current page address (URL)
    • to redirect the browser to another page
    • to reload the same page

    window: in JavaScript represents an open window in a browser.

    location: in JavaScript holds information about current URL.

    The location object is like a fragment of the window object and is called up through the window.location property.

    location object has three methods:

    1. assign(): used to load a new document
    2. reload(): used to reload current document
    3. replace(): used to replace current document with a new one

    So here we need to use reload(), because it can help us in reloading the same document.

    So use it like window.location.reload();.

    Online demo on jsfiddle

    To ask your browser to retrieve the page directly from the server not from the cache, you can pass a true parameter to location.reload(). This method is compatible with all major browsers, including IE, Chrome, Firefox, Safari, Opera.

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