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

问题描述 投票:80回答:14

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

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

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

jquery refresh reload
14个回答
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内容每五秒钟刷新一次。比刷新整个页面更好。


0
投票

请改用它

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

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

0
投票

试试这段代码:

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

0
投票

试试这个:

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

0
投票

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

location.reload();

0
投票

你可能需要使用this参考和location.reload()检查这一点,它会工作。

this.location.reload();

66
投票

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

window.location.reload(true);

40
投票
window.location.href=window.location.href;

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

   location.reload();
        });

这是正确的语法。

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

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条件,页面将只重新加载一次。


3
投票

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

试试这个:

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

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>

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

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做到这一点。

© www.soinside.com 2019 - 2024. All rights reserved.