jQuery Auto refresh div messing up

心已入冬 提交于 2019-12-11 13:47:30

问题


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:

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


回答1:


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>



回答2:


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.



来源:https://stackoverflow.com/questions/13005420/jquery-auto-refresh-div-messing-up

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!