使用jQuery刷新(重新加载)一次页面?

Pet*_*ete 80 jquery refresh reload

我想知道如何使用jQuery刷新/重新加载页面(甚至特定div)一次(!)?

理想情况下,在DOM structure可用之后(参见onload事件),并且不会产生负面影响back buttonbookmark功能.

请注意:replace() 由于第三方限制,不允许:

Ben*_*Ben 79

好吧,我想我得到了你所要求的.试试这个

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.
}
Run Code Online (Sandbox Code Playgroud)

如果它是一次,那就做

$('#div-id').triggerevent(function(){
    $('#div-id').html(newContent);
});
Run Code Online (Sandbox Code Playgroud)

如果是定期的

function updateDiv(){
    //Get new content through Ajax
    ...
    $('#div-id').html(newContent);
}

setInterval(updateDiv, 5000); // That's five seconds
Run Code Online (Sandbox Code Playgroud)

因此,div#div-id内容每五秒钟刷新一次.比刷新整个页面更好.


Mil*_*lli 66

要重新加载,您可以尝试这样做:

window.location.reload(true);
Run Code Online (Sandbox Code Playgroud)

  • location.reload函数的参数确定浏览器是否应该从Web服务器检索文档.如果需要再次从Web服务器提取文档(例如文档内容动态更改的位置)将参数传递为"true"http://grizzlyweb.com/webmaster/javascripts/refresh.asp (7认同)
  • 由于某些原因,这在chrome和safari中不起作用. (2认同)

Maz*_*Maz 40

window.location.href=window.location.href;
Run Code Online (Sandbox Code Playgroud)

  • <讽刺>此代码实际上不使用jQuery </ irony> ;-) (14认同)

小智 5

用这个:

    <script type="text/javascript">
        $(document).ready(function(){

            // Check if the current URL contains '#'
            if(document.URL.indexOf("#")==-1)
            {
                // Set the URL to whatever it was plus "#".
                url = document.URL+"#";
                location = "#";

                //Reload the page
                 location.reload(true);
            }
        });
    </script>
Run Code Online (Sandbox Code Playgroud)

由于这种if情况,页面只会重新加载一次。