如何使用jQuery刷新页面?

JavaScript

樱小小Tom

2020-03-09

如何使用jQuery刷新页面?

第186篇《如何使用jQuery刷新页面?》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

20个回答
小小JinJin蛋蛋 2020.03.09

以下是几行代码,您可以使用这些代码使用jQuery重新加载页面

它使用jQuery包装器并提取本地dom元素。

如果您只想在代码上使用jQuery,并且不关心代码的速度/性能,请使用此方法。

只需选择1到10即可满足您的需求,或者在此之前根据模式和答案添加更多内容。

<script>
  $(location)[0].reload(); //1
  $(location).get(0).reload(); //2
  $(window)[0].location.reload(); //3
  $(window).get(0).location.reload(); //4
  $(window)[0].$(location)[0].reload(); //5
  $(window).get(0).$(location)[0].reload(); //6
  $(window)[0].$(location).get(0).reload(); //7
  $(window).get(0).$(location).get(0).reload(); //8
  $(location)[0].href = ''; //9
  $(location).get(0).href = ''; //10
  //... and many other more just follow the pattern.
</script>
Near米亚 2020.03.09
$(document).on("click", "#refresh_btn", function(event) 
{
     window.location.replace(window.location.href);
});
古一斯丁猴子 2020.03.09

在JavaScript中最短。

window.location = '';
猿GO 2020.03.09

您可以用两种方式编写它。第一种是重新加载页面的标准方法,也称为简单刷新

location.reload(); //simple refresh

另一个叫做硬刷新在这里,您传递布尔表达式并将其设置为true。这将重新加载页面,破坏较旧的缓存并从头开始显示内容。

location.reload(true);//hard refresh
A小卤蛋Pro 2020.03.09

可能最短(12个字符)-使用历史记录

history.go()
凯小胖 2020.03.09

有很多方法可以重新加载当前页面,但是以某种方式使用这些方法,您可以看到页面已更新,但缓存值很少,因此,请解决该问题,或者如果您希望进行艰难的请求,请使用以下代码。

    location.reload(true);
    //Here, it will make a hard request or reload the current page and clear the cache as well.


    location.reload(false); OR location.reload();
    //It can be reload the page with cache
JinJin老丝 2020.03.09

简单的Javascript解决方案:

 location = location; 

<button onClick="location = location;">Reload</button>

GO神奇 2020.03.09

onclick="return location.reload();"在按钮标签内使用

<button id="refersh-page" name="refersh-page" type="button" onclick="return location.reload();">Refesh Page</button>
小小十三泡芙 2020.03.09

采用

location.reload();

要么

window.location.reload();
阿飞小哥 2020.03.09

如果您使用的是jQuery并想刷新,请尝试在javascript函数中添加jQuery:

我想在单击oh an时在页面上隐藏iframe h3,对我来说这是可行的,但iframe除非手动刷新浏览器,否则我无法单击允许我查看该内容的项目,这不理想。

我尝试了以下方法:

var hide = () => {
    $("#frame").hide();//jQuery
    location.reload(true);//javascript
};

将普通Jane javascript与jQuery混合应该可以。

// code where hide (where location.reload was used)function was integrated, below    
    iFrameInsert = () => {
        var file = `Fe1FVoW0Nt4`;
        $("#frame").html(`<iframe width=\"560\" height=\"315\" src=\"https://www.youtube.com/embed/${file}\" frameborder=\"0\" allow=\"autoplay; encrypted-media\" allowfullscreen></iframe><h3>Close Player</h3>`);
        $("h3").enter code hereclick(hide);
}

// View Player 
$("#id-to-be-clicked").click(iFrameInsert);
LEYJim 2020.03.09

不需要 jQuery的任何东西,就可以使用纯JavaScript重新加载页面,只需对location属性使用reload函数,如下所示:

window.location.reload();

默认情况下,这将使用浏览器缓存(如果存在)重新加载页面...

如果您想强制重新加载页面,只需将一个传递给reload方法,如下所示...

window.location.reload(true);

另外,如果您已经在window作用域内,则可以摆脱window并执行以下操作:

location.reload();
Jim猪猪 2020.03.09

我发现

window.location.href = "";

要么

window.location.href = null;

还可以刷新页面。

这使得重新加载页面消除任何哈希值变得非常容易。当我在iOS模拟器中使用AngularJS时,这非常好,这样就不必重新运行该应用程序。

达蒙老丝 2020.03.09

三种与缓存相关行为不同的方法:

  • location.reload(true)

    在实现的forcedReload参数的浏览器location.reload(),通过获取页​​面及其所有资源(脚本,样式表,图像等)的新副本来重新加载。不会从缓存中提供任何资源-从服务器获取新副本,而不在请求中发送任何副本if-modified-sinceif-none-match标头。

    等效于用户在可能的情况下在浏览器中执行“硬重装”。

    请注意,在Firefox(请参阅MDN)和Internet Explorer(请参阅MSDN)中支持传递true但不受普遍支持,也不是W3 HTML 5规范W3 HTML 5.1草案规范WHATWG HTML Living Standard的一部分location.reload()

    在不支持的浏览器(例如Google Chrome)中,location.reload(true)其行为与相同location.reload()

  • location.reload() 要么 location.reload(false)

    重新加载页面,获取页面HTML本身的最新非缓存副本,并对浏览器已缓存的任何资源(例如脚本)执行RFC 7234重新验证请求,即使这些资源是新鲜的,RFC 7234仍允许浏览器提供服务他们没有重新验证。

    location.reload()据我所知,确切地说,浏览器在执行调用时应如何利用其缓存我通过实验确定了上述行为。

    这等效于用户只需在其浏览器中按下“刷新”按钮即可。

  • location = location(或涉及赋予location其属性的无限多种其他可能的技术

    仅在页面的URL不包含虚假/散列的情况下才有效!

    重新加载页面,而无需重新获取或重新验证缓存中的任何 资源。如果页面的HTML本身是最新的,这将重新加载页面而不执行任何HTTP请求。

    从缓存角度看,这等效于用户在新标签页中打开页面。

    但是,如果页面的URL包含哈希,则该哈希将无效。

    再次,据我所知,这里的缓存行为是不确定的。我通过测试确定了它。

因此,总而言之,您要使用:

  • location = location为了最大程度地利用缓存,只要页面的URL中没有哈希即可,在这种情况下,它将无法正常工作
  • location.reload(true)无需重新验证即可获取所有资源的新副本(尽管不受普遍支持,并且location.reload()在某些浏览器(如Chrome)中也不会有所不同
  • location.reload() 忠实地再现用户单击“刷新”按钮的效果。
乐Near 2020.03.09

您可能要使用

location.reload(forceGet)

forceGet 是布尔值和可选值。

默认值为false,它将从缓存中重新加载页面。

如果要强制浏览器从服务器获取页面以摆脱缓存,则将此参数设置为true。

要不就

location.reload()

如果您想快速轻松地进行缓存。

null 2020.03.09

window.location.reload() 将从服务器重新加载,并再次加载所有数据,脚本,图像等。

因此,如果您只想刷新HTML,window.location = document.URL则它们返回的速度会更快,并且流量也更少。但是,如果URL中包含井号(#),它将不会重新加载页面。

null 2020.03.09

要使用jQuery重新加载页面,请执行以下操作:

$.ajax({
    url: "",
    context: document.body,
    success: function(s,x){
        $(this).html(s);
    }
});

我在这里使用的方法是Ajax jQuery。我在Chrome 13 上进行了测试。然后将代码放入处理程序中,该处理程序将触发重新加载。URL"",这意味着这个页面

MandyTony 2020.03.09

如果当前页面是通过POST请求加载的,则可能要使用

window.location = window.location.pathname;

代替

window.location.reload();

因为window.location.reload()如果在由POST请求加载的页面上被调用,则会提示您进行确认。

西里Near 2020.03.09

问题应该是

如何使用JavaScript刷新页面

window.location.href = window.location.href; //This is a possibility
window.location.reload(); //Another possiblity
history.go(0); //And another

您选择太多了。

番长LA 2020.03.09

我想有很多方法可以起作用:

  • window.location.reload();
  • history.go(0);
  • window.location.href=window.location.href;
GONear 2020.03.09

即使没有jQuery,这也应适用于所有浏览器:

location.reload();

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android