【问题标题】:Local storage in javascript that supports both browsers (Chrome and Firefox)支持两种浏览器(Chrome 和 Firefox)的 javascript 本地存储
【发布时间】:2011-08-04 20:04:32
【问题描述】:

当我重新加载同一页面时,我无法在 Firefox 4 网络浏览器中检索我的本地存储值。

注意:下面的代码在最新的 Google Chrome 网络浏览器中运行良好

我的代码:

//Set item
var bookmark_value = document.getElementById('bookmark').value;
var storageIndex = "Bookmarked_Page_" + i;              
localStorage[storageIndex] = bookmark_value;

//get item
document.bookmark["bookmark"].value = localStorage["Bookmarked_Page_" + i];

【问题讨论】:

  • document.bookmark 未定义。
  • 我只是在文本框中获取本地存储项。
  • local storage in IE 的可能副本。这看起来一模一样。你能解释一下为什么@Darin Dimitrov 对这个问题的回答不够充分吗?

标签: javascript firefox google-chrome cross-browser local-storage


【解决方案1】:

错别字?

document.bookmark["bookmark"].value

应该是

document.getElementById('bookmark').value

编辑

哦,我认为这是你的问题,看看这个问题和答案:Is "localStorage" in Firefox only working when the page is online?

【讨论】:

  • 是的,你是对的..我的查询是:当我在 firefox4 中重新加载页面时,本地存储值被清除..你能指导我
  • 仅供参考:我在 file:/// (URL) 中执行示例模块。
  • 嗨..我现在已经解决了这个问题..我想和你分享这个想法,我只是将 html 文件移动到 xampp 服务器中..我将 ulr 设置为localhost/$filename.html..谢谢
【解决方案2】:

您不应依赖浏览器将带有 ID 的元素直接添加到 document

//get item
document.getElementById('bookmark').value = localStorage["Bookmarked_Page_" + i];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-19
    • 1970-01-01
    • 2011-02-20
    相关资源
    最近更新 更多