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

时间:2010-04-01 00:47:14

标签: jquery refresh reload

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

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

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

14 个答案:

答案 0 :(得分: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.
}

如果是一次,那就去做

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

如果是定期

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

setInterval(updateDiv, 5000); // That's five seconds

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

答案 1 :(得分:66)

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

window.location.reload(true);

答案 2 :(得分:40)

window.location.href=window.location.href;

答案 3 :(得分:4)

$('#div-id').click(function(){

   location.reload();
        });

这是正确的语法。

$('#div-id').html(newContent); //This doesnt work

答案 4 :(得分:4)

使用此:

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

由于if条件,页面只会重新加载一次。

答案 5 :(得分:3)

您没有jQuery刷新功能,因为这是JavaScript基础知识。

试试这个:

<body onload="if (location.href.indexOf('reload')==-1) location.replace(location.href+'?reload');">

答案 6 :(得分:2)

使用:

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

答案 7 :(得分:1)

将其添加到文件顶部,该网站每30秒刷新一次。

<script type="text/javascript">
    window.onload = Refresh;

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

另一个是:添加头标记

<meta http-equiv="refresh" content="30">

答案 8 :(得分:1)

 - 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']

你不需要jQuery。你可以用JavaScript做到这一点。

答案 9 :(得分:0)

试试这段代码:

$('#iframe').attr('src', $('#iframe').attr('src'));

答案 10 :(得分:0)

改为使用

function timedRefresh(timeoutPeriod) {
    setTimeout("location.reload(true);",timeoutPeriod);
}

<a href="javascript:timedRefresh(2000)">image</a>

答案 11 :(得分:0)

使用javascript刷新页面时,您只需使用:

location.reload();

答案 12 :(得分:0)

试试这个:

<input type="button" value="Reload" onClick="history.go(0)">

答案 13 :(得分:0)

您可能需要使用this引用以及location.reload() 检查一下,它会起作用。

this.location.reload();
相关问题