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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2557480/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 13:47:15  来源:igfitidea点击:

Refresh (reload) a page once using jQuery?

jqueryrefreshreload

提问by Pete

I'm wondering how to refresh/reload a page (or even specific div) once(!) using jQuery?

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

Ideally in a way right after the DOM structureis available (cf. onloadevent) and not negatively affecting back buttonor bookmarkfunctionality.

理想情况下,在DOM structure可用之后(参见onload事件)的方式,而不是负面影响back buttonbookmark功能。

Please, note: replace()is not allowed due to third-party restrictions.

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

采纳答案by Ben

Alright, I think I got what you're asking for. Try this

好吧,我想我得到了你要的东西。尝试这个

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


If it is once, then just do

如果是一次,那么就做

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

If it is periodically

如果是定期

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

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

So, every five seconds the div #div-id content will refresh. Better than refreshing the whole page.

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

回答by Milli

To reload, you can try this:

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

window.location.reload(true);

回答by Maz

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

回答by Parag Gaiwkad

Use this:

用这个:

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

Due to the ifcondition, the page will reload only once.

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

回答by swapnil

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

   location.reload();
        });

This is the correct syntax.

这是正确的语法。

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

回答by Adrian P.

You don't have a jQuery refresh function, because this is JavaScript basics.

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

Try this:

尝试这个:

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

回答by Vicky

Use:

用:

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

回答by Paresh3489227

Add this to the top of your file and the site will refresh every 30 seconds.

将此添加到文件顶部,站点将每 30 秒刷新一次。

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

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

Another one is: Add in the head tag

另一种是:在head标签中添加

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

回答by Steve

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

You don't need jQuery for this. You can do it with JavaScript.

为此,您不需要 jQuery。你可以用 JavaScript 做到这一点。

回答by Tino Jose Thannippara

You may need to use thisreference along with location.reload()check this, it will work.

您可能需要使用this参考并location.reload()检查它,它会起作用。

this.location.reload();