使用 JavaScript 或 HTML 刷新页面

发布于 2024-10-21 22:32:39 字数 35 浏览 7 评论 0原文

如何使用 JavaScript 或 HTML 刷新页面?

How can I refresh a page using JavaScript or HTML?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

呆橘 2024-10-28 22:32:39

JavaScript 中的 window.location.reload();

HTML 中的 (其中 1< /code> = 1 秒)。

window.location.reload(); in JavaScript

<meta http-equiv="refresh" content="1"> in HTML (where 1 = 1 second).

失而复得 2024-10-28 22:32:39

这里有 535 种使用 JavaScript 重新加载页面的方法,非常酷:

这里是前 20 个:

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']
location = window.location['href']
location = self['location']
location = self['location'].href
location = self['location']['href']
location = self.location['href']
location.assign(location)
location.replace(location)
window.location.assign(location)
window.location.replace(location)
self.location.assign(location)

和最后 10 个:

self['location']['replace'](self.location['href'])
location.reload()
location['reload']()
window.location.reload()
window['location'].reload()
window.location['reload']()
window['location']['reload']()
self.location.reload()
self['location'].reload()
self.location['reload']()
self['location']['reload']()

原创文章

Here are 535 ways to reload a page using javascript, very cool:

Here are the first 20:

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']
location = window.location['href']
location = self['location']
location = self['location'].href
location = self['location']['href']
location = self.location['href']
location.assign(location)
location.replace(location)
window.location.assign(location)
window.location.replace(location)
self.location.assign(location)

and the last 10:

self['location']['replace'](self.location['href'])
location.reload()
location['reload']()
window.location.reload()
window['location'].reload()
window.location['reload']()
window['location']['reload']()
self.location.reload()
self['location'].reload()
self.location['reload']()
self['location']['reload']()

Original Article

烟花易冷人易散 2024-10-28 22:32:39

只需使用..

location.reload(true/false);

如果为 false,则页面将从缓存中重新加载,否则从服务器中重新加载。

simply use..

location.reload(true/false);

If false, the page will be reloaded from cache, else from the server.

飘逸的'云 2024-10-28 22:32:39
window.location.reload()

应该可以,但是有很多不同的选择,例如:

window.location.href=window.location.href
window.location.reload()

should work however there are many different options like:

window.location.href=window.location.href
放低过去 2024-10-28 22:32:39

您也可以使用

<input type="button" value = "Refresh" onclick="history.go(0)" />

它对我来说效果很好。

You can also use

<input type="button" value = "Refresh" onclick="history.go(0)" />

It works fine for me.

暗恋未遂 2024-10-28 22:32:39

使用:

window.location.reload();

Use:

window.location.reload();
沫尐诺 2024-10-28 22:32:39

如果它有一些东西可以控制缓存页面上的更新,我有一个很好的方法来做到这一点。

  • 向页面添加一些 javascript,以便在加载时始终以字符串 (ajax) 形式获取页面的版本号。例如:www.yoursite.com/page/about?getVer=1&__[date]
  • 如果用户访问过该页面一次,则将其与存储的版本号(存储在 cookie 或 localStorage 中)进行比较,否则直接存储。
  • 如果版本与本地版本不同,请使用 window.location.reload(true) 刷新页面,
  • 您将看到页面上所做的任何更改。

即使不需要请求,此方法也至少需要一个请求,因为它已经存在于本地浏览器缓存中。但与根本不使用缓存相比,开销更少(以确保页面将显示正确的更新内容)。这仅需要每个页面请求几个字节,而不是每个页面的所有内容。

重要提示:版本信息请求必须在您的服务器上实现,否则它将返回整个页面。

www.yoursite.com/page/about?getVer=1&__[date] 返回的版本字符串示例:
skg2pl-v8kqb

为了给你一个代码示例,这是我的库的一部分(我认为你不能使用它,但也许它会给你一些如何做的想法):

o.gCheckDocVersion = function() // Because of the hard caching method, check document for changes with ajax 
{
  var sUrl = o.getQuerylessUrl(window.location.href),
      sDocVer = o.gGetData( sUrl, false );

  o.ajaxRequest({ url:sUrl+'?getVer=1&'+o.uniqueId(), cache:0, dataType:'text' }, 
                function(sVer)
                {
                   if( typeof sVer == 'string' && sVer.length )
                   {
                     var bReload = (( typeof sDocVer == 'string' ) && sDocVer != sVer );
                     if( bReload || !sDocVer )
                      { 
                        o.gSetData( sUrl, sVer ); 
                        sDocVer = o.gGetData( sUrl, false );
                        if( bReload && ( typeof sDocVer != 'string' || sDocVer != sVer ))
                         { bReload = false; }
                      }
                     if( bReload )
                      {  // Hard refresh page contents
                        window.location.reload(true); }
                   }
                }, false, false );
};

如果你使用的是版本无关的资源,比如 javascript或 css 文件,添加版本号(通过 url 重写实现,而不是通过查询实现,因为它们大多不会被缓存)。例如:www.yoursite.com/ver-01/about.js

对我来说,这个方法效果很好,也许它也可以帮助你。

If it has something to do control updates on cached pages here I have a nice method how to do this.

  • add some javascript to the page that always get the versionnumber of the page as a string (ajax) at loading. for example: www.yoursite.com/page/about?getVer=1&__[date]
  • Compare it to the stored versionnumber (stored in cookie or localStorage) if user has visited the page once, otherwise store it directly.
  • If version is not the same as local version, refresh the page using window.location.reload(true)
  • You will see any changes made on the page.

This method requires at least one request even when no request is needed because it already exists in the local browser cache. But the overhead is less comparing to using no cache at all (to be sure the page will show the right updated content). This requires just a few bytes for each page request instead of all content for each page.

IMPORTANT: The version info request must be implemented on your server otherwise it will return the whole page.

Example of version string returned by www.yoursite.com/page/about?getVer=1&__[date]:
skg2pl-v8kqb

To give you an example in code, here is a part of my library (I don't think you can use it but maybe it gives you some idea how to do it):

o.gCheckDocVersion = function() // Because of the hard caching method, check document for changes with ajax 
{
  var sUrl = o.getQuerylessUrl(window.location.href),
      sDocVer = o.gGetData( sUrl, false );

  o.ajaxRequest({ url:sUrl+'?getVer=1&'+o.uniqueId(), cache:0, dataType:'text' }, 
                function(sVer)
                {
                   if( typeof sVer == 'string' && sVer.length )
                   {
                     var bReload = (( typeof sDocVer == 'string' ) && sDocVer != sVer );
                     if( bReload || !sDocVer )
                      { 
                        o.gSetData( sUrl, sVer ); 
                        sDocVer = o.gGetData( sUrl, false );
                        if( bReload && ( typeof sDocVer != 'string' || sDocVer != sVer ))
                         { bReload = false; }
                      }
                     if( bReload )
                      {  // Hard refresh page contents
                        window.location.reload(true); }
                   }
                }, false, false );
};

If you are using version independent resources like javascript or css files, add versionnumbers (implemented with a url rewrite and not with a query because they mostly won't be cached). For example: www.yoursite.com/ver-01/about.js

For me, this method is working great, maybe it can help you too.

九命猫 2024-10-28 22:32:39

试试这个工作正常

jQuery("body").load(window.location.href);

try this working fine

jQuery("body").load(window.location.href);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文