如何使用JavaScript来检测我是否在缓存页面上 [英] How can I use JavaScript to detect if I am on a cached page

查看:119
本文介绍了如何使用JavaScript来检测我是否在缓存页面上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从缓存中显示很长时间,我希望页面渲染与从缓存加载页面略有不同。有一种简单的方法我可以用JavaScript确定这个吗?

I want to display from cache for a long time and I want a slightly different behavior on page render vs loading the page from cache. Is there an easy way I can determine this with JavaScript?

推荐答案

你可以做的一种方法是包括页面的时间在页面中生成,然后使用一些javascript将本地时间与页面生成时间进行比较。如果时间与阈值不同,则页面来自缓存。问题是如果客户端计算机的时间设置不正确,尽管您可以通过使客户端在请求中包含其当前系统时间来生成页面然后将该值发送回客户端来解决此问题。

One way you could do it is to include the time the page was generated in the page and then use some javascript to compare the local time to the time the page was generated. If the time is different by a threshold then the page has come from a cache. The problem with that is if the client machine has its time set incorrectly, although you could get around this by making the client include its current system time in the request to generate the page and then send that value back to the client.

这篇关于如何使用JavaScript来检测我是否在缓存页面上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆