只有在DOM准备好后,localStorage才能完全加载吗? [英] Is localStorage fully loaded only after DOM is ready?

查看:98
本文介绍了只有在DOM准备好后,localStorage才能完全加载吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些javascript代码的错误,很难重现(所以没有jsfiddle,对不起)。完全相同的浏览器(FF 37.0.2),但两台不同的机器,我只能重现其中一台的错误。

I have got a bug with some javascript code that it's hard to reproduce (so no jsfiddle, sorry). Exactly the same browser (FF 37.0.2), but two different machines, and I can only reproduce the bug on one of them.

我怀疑这个问题有什么问题使用localStorage以及我检查项目是否在$(document).ready()之外的事实。

I suspect the issue has something to do with localStorage and the fact that I check if an item is there outside $(document).ready().

这是否需要?在可靠地访问localStorage之前,是否需要等待DOM准备好?我的假设是否合理?

Is that required? Do I need to wait for the DOM to be ready before reliably accessing localStorage? Is my hypothesis plausible?

推荐答案

localStorage不是需要异步加载的东西。它在页面开始加载时可用,并且可以由页面中任何位置的Javascript使用。如果浏览器必须从某个地方(例如磁盘)获取值,那么在您请求数据时或之前同步完成。

localStorage is not something that needs to be "loaded" asynchronously. It is available the moment the page starts to load and can be used by Javascript anywhere in the page. If the browser has to fetch values form somewhere (e.g. the disk), that is done synchronously when you request the data or before.

无需等待 $(document).ready()访问localStorage之前。你的问题的原因必须是别的。

There is no need to wait with $(document).ready() before accessing localStorage. The cause of your issue must be something else.

仅供参考,你可以在这里阅读WebStorage的规范: http://dev.w3.org/html5/webstorage/#dom-localstorage 。该文档的localStorage部分中没有任何迹象表明JS代码必须在访问之前等待。

FYI, you can read the spec on WebStorage here: http://dev.w3.org/html5/webstorage/#dom-localstorage. There is no indication in the localStorage section of that document that JS code must "wait" before accessing.

这篇关于只有在DOM准备好后,localStorage才能完全加载吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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