简体   繁体   中英

jQuery Auto refresh div messing up

I have a script that auto-refreshes a certain div on the page (That I got from another post on here)

<script type="text/javascript">
    var auto_refresh = setInterval(
    function(){
        $('#refresh').load('index.php?_=' +Math.random()).fadeIn("slow");
    }, 10000); // refresh every 10000 milliseconds
</script>
...............
<div id="refresh">
  <!-- Some PHP Code -->
</div>

This refreshes, however when it does, I takes the entire html document and puts it into the div. Like this:

div中的主体

As you can see, the refreshed div (the one marked in red) is getting the body shouved into it. Any ideas???

First off, you are loading the entire page into the divider, thus causing the file to reload entirely. Instead, you should be having the Recent Posts divider load from a single file, even on the first page load. Then have that consistently refresh over time.

Secondly, you should be transferring as little data as possible from your server to your clients. At most, you should use a minimalistic checksum of sorts (number of messages, for instance) to confirm that the client and server are synced up.

Lastly, if you choose to use this format, aim to transfer your data in something such as JSON or XML and have the client display it on the page. Transferring the styled HTML increases network overhead and is not the best practice.

You are loading entire page to the div. Modify the code to use only part of the document that is fetched:

    <script type="text/javascript">
        var auto_refresh = setInterval(
        function(){
            $('#refresh').empty();
            $('#refresh').load('index.php?_=' +Math.random()+' #refresh').fadeIn("slow");
        }, 10000); // refresh every 10000 milliseconds
    </script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM