最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery Auto refresh div messing up - Stack Overflow

programmeradmin2浏览0评论

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???

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???

Share Improve this question asked Oct 22, 2012 at 5:29 Kyle MorganKyle Morgan 6901 gold badge11 silver badges21 bronze badges 2
  • try after changing $('#refresh') to $(document) – xkeshav Commented Oct 22, 2012 at 5:32
  • Please tell us your HTML structure for better answer – xkeshav Commented Oct 22, 2012 at 5:34
Add a ment  | 

2 Answers 2

Reset to default 4

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>

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.

发布评论

评论列表(0)

  1. 暂无评论