Refresh (reload) a page once using jQuery?

前端 未结 14 1756
眼角桃花
眼角桃花 2020-11-30 22:34

I\'m wondering how to refresh/reload a page (or even specific div) once(!) using jQuery?

Ideally in a way right after the DOM structure is available (c

相关标签:
14条回答
  • 2020-11-30 22:36

    Use:

    <html>
        <head>
            <title>Reload (Refresh) Page Using Jquery</title>
            <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
            <script type="text/javascript">
                $(document).ready(function () {
                    $('#reload').click(function() {
                        window.location.reload();
                    });
                });
            </script>
        </head>
        <body>
            <button id="reload" >Reload (Refresh) Page Using</button>
        </body>
    </html>
    
    0 讨论(0)
  • 2020-11-30 22:37
    $('#div-id').click(function(){
    
       location.reload();
            });
    

    This is the correct syntax.

    $('#div-id').html(newContent); //This doesnt work
    
    0 讨论(0)
  • 2020-11-30 22:37

    For refreshing page with javascript, you can simply use:

    location.reload();
    
    0 讨论(0)
  • 2020-11-30 22:38
     - 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']
    

    You don't need jQuery for this. You can do it with JavaScript.

    0 讨论(0)
  • 2020-11-30 22:40

    Add this to the top of your file and the site will refresh every 30 seconds.

    <script type="text/javascript">
        window.onload = Refresh;
    
        function Refresh() {
            setTimeout("refreshPage();", 30000);
        }
        function refreshPage() {
            window.location = location.href;
        }
    </script>
    

    Another one is: Add in the head tag

    <meta http-equiv="refresh" content="30">
    
    0 讨论(0)
  • 2020-11-30 22:41

    Alright, I think I got what you're asking for. Try this

    if(window.top==window) {
        // You're not in a frame, so you reload the site.
        window.setTimeout('location.reload()', 3000); //Reloads after three seconds
    }
    else {
        //You're inside a frame, so you stop reloading.
    }
    

    If it is once, then just do

    $('#div-id').triggerevent(function(){
        $('#div-id').html(newContent);
    });
    

    If it is periodically

    function updateDiv(){
        //Get new content through Ajax
        ...
        $('#div-id').html(newContent);
    }
    
    setInterval(updateDiv, 5000); // That's five seconds
    

    So, every five seconds the div #div-id content will refresh. Better than refreshing the whole page.

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