简体   繁体   English

我可以从javascript告诉我的页面是否刷新了吗?

[英]Can I tell from javascript whether my page was hard refreshed?

I've given up on this, but I thought I'd post here out of curiosity. 我已经放弃了这一点,但我想我会因为好奇而在这里发帖。

What I call a "hard refresh" is the Ctrl+R or Shift+F5 that you do during development to see your changes. 我称之为“硬刷新”的是您在开发过程中执行的Ctrl + RShift + F5 ,以查看您的更改。

This causes the browser to add a Cache-Control: max-age=0 header to the request and "child" requests like images and scripts, etc. 这会导致浏览器向请求添加Cache-Control: max-age=0标头, 并为图像和脚本等添加“子”请求。

If you're doing your job, you'll get a 304 on everything but the resource that's changed. 如果你正在做你的工作,除了改变的资源外,你将得到304 (Okay, well, see comments. This is assuming that other validators are sent based on browser caches.) (好的,请参阅注释。这是假设其他验证器基于浏览器缓存发送。)

So far, so good. 到现在为止还挺好。

The problem is that I'm not loading scripts directly from the page, but through a load.js , and the browsers are inconsistent about whether they include that Cache-Control header on those requests. 问题是我不是直接从页面加载脚本,而是通过load.js加载脚本,并且浏览器在这些请求中是否包含该Cache-Control标头时不一致。 Chrome doesn't do it at all, and Firefox seems to stop in the middle of a series. Chrome根本不会这样做,Firefox似乎停在了系列的中间。

Since I can't access the headers of the current request, there's no way to know whether that header should be included or not. 由于我无法访问当前请求的标头,因此无法知道是否应包含该标头。

The result is that when I change a script (other than load.js ), a hard refresh does not reliably work, and I have to, eg, clear the browser cache (which is a bit heavy-handed). 结果是,当我更改脚本(除了load.js )之外,硬刷新不能可靠地工作,我必须,例如,清除浏览器缓存(这有点沉重)。

Any thoughts on this? 有什么想法吗?

Unfortunately you cannot detect a hard refresh from JavaScript (there is no access to the headers for the currently loaded page). 遗憾的是,您无法从JavaScript检测到硬刷新(无法访问当前加载页面的标头)。

However, the server can tell from the request headers if this is a hard refresh, so there's the option of cooperating. 但是,服务器可以从请求标头中判断这是否是硬刷新,因此可以选择合作。 For example the server can include a custom <meta> tag in the response or add a special class to <body> and your script will then have access to this information. 例如,服务器可以在响应中包含自定义<meta>标记,或者向<body>添加一个特殊类,然后您的脚本就可以访问此信息。

Once load.js detects a hard refresh it can then propagate it to the dependent scripts by eg attaching a URL parameter to the requests (think "?t=" + timestamp ). 一旦load.js检测到硬刷新,它就可以通过例如将URL参数附加到请求来将其传播到依赖脚本(想想"?t=" + timestamp )。

You could try checking localStorage. 您可以尝试检查localStorage。 Set a localStorage variable and check it. 设置localStorage变量并进行检查。 If it's there, it's not a hard refresh, otherwise, it is a hard refresh. 如果它在那里,它不是一个硬刷新,否则,它是一个很难刷新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何判断“我的页面上是否存在 Javascript object”? - How can I tell 'Whether a Javascript object exists on my page'? 如何判断ReactJS是否处于JavaScript开发模式? - How can I tell whether ReactJS is in development mode from JavaScript? 如何判断JavaScript文件是否已包含在ASP.NET页面中? - How can I tell whether a JavaScript file has already been included in an ASP.NET page? 我的javascript函数只有在我刷新页面后才能工作 - My javascript function works only after I hard refresh the page 为什么在单击使用JavaScript的输入按钮时,我的asp.net页已刷新? - Why on click of a input button using JavaScript i am getting my asp.net page refreshed? 刷新页面时如何继续使用 javascript 添加的动画? - How can I continue the animations I added with javascript when the page is refreshed? 您能否通过PHP判断ajax调用是来自页面还是来自控制台? - Can you tell, via PHP, whether an ajax call came from a page or the console? 如何判断页面是否已跳转到javascript中的锚点(#)? - How can I tell if a page has jumped to the anchor (#) in javascript? 如何使用Javascript来判断浏览器是否更改了HTML输入字段? - How can I use Javascript to tell whether an HTML input field has been changed by the browser? Javascript刷新页面,我不知道为什么 - Javascript refreshing page, I can't tell why
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM