简体   繁体   English

如何在浏览器中存储缓存?

[英]How to store cache in browser?

I want to store data in cache at browser side. 我想将数据存储在浏览器端的缓存中。 But If I close the browser and I get back to my application I should get my Cached Data back, 但是,如果我关闭浏览器并返回到我的应用程序,则应该取回我的缓存数据,

Is this possible? 这可能吗?

If Yes then How? 如果是,那么如何?

If you are happy with the browser support (check http://caniuse.com/#feat=namevalue-storage ), you can use local storage to achieve this, which is much more powerful than cookies. 如果您对浏览器支持感到满意(请访问http://caniuse.com/#feat=namevalue-storage ),则可以使用本地存储来实现此功能,它比cookie强大得多。

There are limits on how much you can store (varies by browser, but assume ~5Mb - compared with 4Kb per cookie), and it has the advantage over cookies that it is not sent to the server with every request. 您可以存储的数量是有限的(因浏览器而异,但假定为〜5Mb-与每个cookie 4Kb相比),并且它具有优于cookie的优点,因为它不会随每个请求发送到服务器。 This adds a (slight) request overhead that you may not want. 这会增加您可能不希望的(少量)请求开销。

Whichever solution you go for, make sure you handle the case that the user has cleared out all this data, or maybe just some of it.... 无论您采用哪种解决方案,请确保处理用户清除了所有这些数据或其中的一部分的情况。

You can use cookies to store data and retrieve it the next time user opens your webpage. 您可以使用Cookie来存储数据,并在用户下次打开您的网页时检索它。

For example - 例如 -

Use this snippet to capture the data of some textfield into a cookie - 使用此代码段将某些文本字段的数据捕获到Cookie中-

   $.cookie("mydata", $(".sometextfield").val(), {expires: 7});

Now suppose the user closes the browser. 现在,假设用户关闭浏览器。

The next time a user visits your page, his snippet will read the cookie data and populate the textfield on page load - 下次用户访问您的网页时,其摘要将读取Cookie数据并在页面加载时填充文本字段-

  $(".sometextfield").val( $.cookie("mydata") );

Use HTML5 local storage. 使用HTML5本地存储。

Ref: http://diveintohtml5.info/storage.html 参考: http : //diveintohtml5.info/storage.html

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM