YUI 2:存储实用程序 - 为什么键/值不逐页保存

发布于 2024-09-12 20:16:39 字数 2806 浏览 3 评论 0原文

尝试使用 YUI 2 Storage Utility 并遵循有效的示例(在同一页面上);但是当我创建第二个页面(例如 page2.html)并尝试访问该密钥时,我得到的值为 null。

因此,在 page1 上,我调用:

localStorageEngine.setItem('testText', 'this is a triumph (local storage engine)');

,如果我在同一页面上使用 getItem,我会看到它,但如果我加上 page2.html:,

alert(localStorageEngine.getItem('testText'));

我会返回 null。我认为这就是重点。以下是我除了 jquery 1.4.2 之外还包含的内容

src="http://yui.yahooapis.com/combo?2.8.1/build/yahoo-dom-event/yahoo-dom-event.js&2.8.1/build/cookie/cookie-min.js&2.8.1/build/element/element-min.js&2.8.1/build/swf/swf-min.js&2.8.1/build/swfstore/swfstore-min.js&2.8.1/build/storage/storage-min.js"

,这是我的大部分 javascript 代码:

//YAHOO.util.Event.onDOMReady(function() {  
jQuery(document).ready(function() {

  var localStorageEngine;
  try {
    localStorageEngine = YAHOO.util.StorageManager.get(
        YAHOO.util.StorageEngineHTML5.ENGINE_NAME,
        YAHOO.util.StorageManager.LOCATION_LOCAL, 
        {   
            force: false,
            order: [
                YAHOO.util.StorageEngineHTML5,
                YAHOO.util.StorageEngineSWF,
                YAHOO.util.StorageEngineGears
            ]
        }
     );
  } catch(e) {
    YAHOO.log("No supported storage mechanism present.");
    localStorageEngine = false;
  }
  if(localStorageEngine) {
    localStorageEngine.subscribe(localStorageEngine.CE_READY, function() {
        localStorageEngine.setItem('testText', 'this is a triumph (local storage engine)');
        localStorageEngine.setItem('testNumber', 1234567890);
        localStorageEngine.setItem('testBoolean', true);
        alert(localStorageEngine.getItem('testText'));

        console.log("len: ", localStorageEngine.length);
        console.log("has key: ", localStorageEngine.hasKey("testText"));
    });
  }
});


编辑:

Eric 的答案(非常有帮助!)有效,但在我的情况下,它是具体的swf 行(不使用 jquery 文档就绪,而不是使用 yahoo 的相同)。这是相关的代码,其他方面几乎相同:


//YAHOO.util.Event.onDOMReady(function() {  
jQuery(document).ready(function() {
  YAHOO.util.StorageEngineSWF.SWFURL = 'assets/swfstore2-8-1.swf';

编辑2 对混乱的编辑表示歉意,但我回到这篇文章,因为我现在需要实现并意识到埃里克示例的第二页(尽管他可能选择将其删除)仍在获取之前设置值,因此它不会并不能真正体现保留页面之间的价值。因此,我将其添加到 get 之前的第二页中,当我对 getItem 发出警报时,它又恢复为 null:

/*
        localStorageEngine.setItem('testText', 'this is a triumph (local storage engine)');
        localStorageEngine.setItem('testNumber', 1234567890);
        localStorageEngine.setItem('testBoolean', true);
*/

实质上注释掉了第 2 页上的设置代码。在阅读有关底层 DOM 存储对象的更多信息后,我发现它与特定域相关联。例如,如果我这样做:

sessionStorage.setItem("name", "Rob");

在我的本地计算机上,我得到一个不允许的操作,但如果我将其上传到服务器,它就可以工作。对存储实用程序示例做了同样的事情,它也有效。所以要点是将其放在服务器上。

Trying to use the YUI 2 Storage Utility and followed the example which worked (on the same page); but when I create a second page (say page2.html) and try to access the key I get null back for the value.

So on page1 one I call:

localStorageEngine.setItem('testText', 'this is a triumph (local storage engine)');

and if I use getItem on the same page I see it, but if I put on say page2.html:

alert(localStorageEngine.getItem('testText'));

I get back null. I thought this was the point. The following is what I've included in addition to jquery 1.4.2

src="http://yui.yahooapis.com/combo?2.8.1/build/yahoo-dom-event/yahoo-dom-event.js&2.8.1/build/cookie/cookie-min.js&2.8.1/build/element/element-min.js&2.8.1/build/swf/swf-min.js&2.8.1/build/swfstore/swfstore-min.js&2.8.1/build/storage/storage-min.js"

Here's the bulk of my javascript code:

//YAHOO.util.Event.onDOMReady(function() {  
jQuery(document).ready(function() {

  var localStorageEngine;
  try {
    localStorageEngine = YAHOO.util.StorageManager.get(
        YAHOO.util.StorageEngineHTML5.ENGINE_NAME,
        YAHOO.util.StorageManager.LOCATION_LOCAL, 
        {   
            force: false,
            order: [
                YAHOO.util.StorageEngineHTML5,
                YAHOO.util.StorageEngineSWF,
                YAHOO.util.StorageEngineGears
            ]
        }
     );
  } catch(e) {
    YAHOO.log("No supported storage mechanism present.");
    localStorageEngine = false;
  }
  if(localStorageEngine) {
    localStorageEngine.subscribe(localStorageEngine.CE_READY, function() {
        localStorageEngine.setItem('testText', 'this is a triumph (local storage engine)');
        localStorageEngine.setItem('testNumber', 1234567890);
        localStorageEngine.setItem('testBoolean', true);
        alert(localStorageEngine.getItem('testText'));

        console.log("len: ", localStorageEngine.length);
        console.log("has key: ", localStorageEngine.hasKey("testText"));
    });
  }
});


EDIT:

Eric's answer (very helpful!) worked but it turned out in my case to be specifically the swf line (not using jquery document ready instead of yahoo's same). Here's the pertinent code which is pretty much otherwise the same:



//YAHOO.util.Event.onDOMReady(function() {  
jQuery(document).ready(function() {
  YAHOO.util.StorageEngineSWF.SWFURL = 'assets/swfstore2-8-1.swf';

EDIT 2
Apologies for the messy edits but I've come back to this post as I now need to implement and realized that the second page on Eric's example (though he may choose to take it down) is still setting the value before the get so it doesn't truly exemplify retaining the value between pages. So I added this to the 2nd page before the get and it went back to being null when I did an alert on the getItem:

/*
        localStorageEngine.setItem('testText', 'this is a triumph (local storage engine)');
        localStorageEngine.setItem('testNumber', 1234567890);
        localStorageEngine.setItem('testBoolean', true);
*/

essentially commenting out the set code on page 2. After reading more about the underlying DOM Storage object I see that it is tied to a particular domain. For example, if I do:

sessionStorage.setItem("name", "Rob");

on my local machine I get an operation not permitted but if I upload it to a server it works. Did the same with the storage utility examples and it also worked. So the gist is put it up on a server.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

つ低調成傷 2024-09-19 20:16:39

Rob,

我设置了两个测试页面,它们基本上是从您的实例化代码中复制粘贴的:

http ://ericmiraglia.com/yui/demos/storage.php

当我从一个转到另一个时,每次都会弹出成功窗口。

我的代码和你的代码之间唯一显着的区别是省略了 jQuery(注意:这最终对功能没有影响)和添加了 SWF 位置(对于不支持 HTML5 存储的浏览器来说,这作为后备很重要;更新:这就是解决 Rob 遇到的问题的方法)。但我在 Safari 5 和 FF 3.6 中进行了测试,所以它应该直接使用 HTML5 支持。

这些页面适合您吗?

-埃里克

Rob,

I set up two test pages which are basically copy-and-paste from your instantiation code:

http://ericmiraglia.com/yui/demos/storage.php

When I go from one to the other I get the success popup each time.

The only significant difference between my code and yours is the omission of jQuery (note: this ultimately has no impact on functionality) and the addition of the SWF location (which is important as a fallback for browsers that don't support HTML5 storage; UPDATE: this is what solved the problem Rob was having). But I tested in Safari 5 and FF 3.6, so it should have just been using HTML5 support directly.

Do those pages work for you?

-Eric

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文