简体   繁体   English

是否可以在HTML5中单独覆盖本地存储和会话存储?

[英]Is it possible to override Local Storage and Session Storage separately in HTML5?

I know it's possible to override the HTML5 storage APIs by overriding Storage.prototype.getItem, setItem, removeItem and clear. 我知道可以通过覆盖Storage.prototype.getItem,setItem,removeItem和clear来覆盖HTML5存储API。 But that will override those methods for both local storage and session storage. 但是,这将覆盖本地存储会话存储的那些方法。

Is it possible to just override one and not the other? 是否可以覆盖一个而不是另一个? Or to override both separately? 或者单独覆盖两个?

A little context: I have an existing app that makes very heavy use of both local storage and session storage. 一点上下文:我有一个现有的应用程序,它大量使用本地存储和会话存储。 I want to add some temporary code to mirror the stuff in local storage in another storage mechanism, but I don't want to drag the session storage contents along with it. 我想添加一些临时代码来镜像另一个存储机制中的本地存储中的东西,但我不想随之拖动会话存储内容。

I could update every reference to localStorage to call some wrapper function that could do the mirroring, but I really don't want to update all those calls. 我可以更新对localStorage的每个引用来调用一些可以执行镜像的包装器函数,但我真的不想更新所有这些调用。 It would be way cleaner if I could localize this code by overriding a single set of storage methods. 如果我可以通过覆盖一组存储方法来本地化这些代码,那将更加清晰。

There are several possibilities to achieve what you want. 有几种可能性来实现你想要的。 But remember, none of them should be used in production environment. 但请记住,它们都不应该用于生产环境。

The first option detects if setItem method was called by sessionStorage or localStorage object. 第一个选项检测sessionStoragelocalStorage对象是否调用了setItem方法。 You could write it this way: 你可以这样写:

var _setItem = Storage.prototype.setItem;

Storage.prototype.setItem = function(key, value) { 
    if (this === window.localStorage) {
         // do what you want if setItem is called on localStorage
    } else {
         // fallback to default action
         _setItem.apply(this, arguments);
    }
}

The second one, replaces prototype of sessionStorage or localStorage object. 第二个,替换sessionStoragelocalStorage对象的原型。 It may look like this: 它可能看起来像这样:

localStorage.__proto__ = Object.create(Storage.prototype);
localStorage.__proto__.setItem = function() {
    // your logic here
}

Notice, that I used __proto__ pseudo property which is non-standard, but exposed in Chrome and Firefox. 请注意,我使用了__proto__伪属性,这是非标准属性,但在Chrome和Firefox中公开。 (Don't know about Opera, Safari and others). (不了解Opera,Safari等)。 However, as you can see it might be helpful during development. 但是,您可以看到它在开发过程中可能会有所帮助。

Yes, Local Storage and Session Storage override separately in HTML5 是的,本地存储和会话存储在HTML5中单独覆盖

localStorage.setItem('name', Krishna); //here local storege value is Krishna
sessionStorage.name = "Krishna"; //here Session storege value is Krishna

In below we override Local Storage and Session Storage value separately 在下面我们分别覆盖本地存储和会话存储值

localStorage.setItem('name', Sri); //here local storege value is Sri
sessionStorage.name = "Pal"; //here Session storege value is Pal

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

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