简体   繁体   English

html5会话存储有哪些替代方法?(非本地存储)

[英]What are the alternatives to html5 session storage?(not local storage)

I want 2 javascript methods to access certain data without passing this data to any of the methods as arguments. 我想要2个javascript方法来访问某些数据,而无需将此数据作为参数传递给任何方法。 Basically I want one method to set the data and other to consume that data using the objects already available in the browser and I want the solution to work with Safari, Firefox, Chrome and IE8+ also iOS and android browsers. 基本上,我想要一种方法来设置数据,而另一种方法是使用浏览器中已经可用的对象来使用该数据,并且我希望该解决方案能够与Safari,Firefox,Chrome和IE8 +以及iOS和android浏览器一起使用。 I believe session storage does not work on iOS. 我认为会话存储在iOS上不起作用。 Is it correct? 这是正确的吗? What are the drawbacks of session storage. 会话存储的缺点是什么? I tried to append the data to the event object but it does not work in firefox and IE. 我试图将数据附加到事件对象,但在Firefox和IE中不起作用。

Although sessionStorage is available for iOS, you can do following for legacy support. 尽管sessionStorage可用于iOS,但您可以执行以下操作以获取旧版支持。 You can use window.name . 您可以使用window.name It lives through the session (ie until the browser tab gets closed): 它通过会话存在(即,直到关闭浏览器选项卡):

var myData = {
  'foo': 'bar',
  'foz': 'baz'
};

window.name = JSON.stringify(myData);

And you can read it back: 您可以将其读回:

var myData = JSON.parse(window.name);

This of course requires JSON support from the browser, which leaves very old browsers out of the scope. 当然,这需要浏览器的JSON支持,这会使非常老的浏览器不在范围之内。

You could use a "global variable" (I put that in quotes because I'm not sure what it's actually called in Javascript). 您可以使用“全局变量”(我在引号中加上了引号,因为我不确定它在Javascript中实际调用了什么)。 But basically you could just make an object in one of your javascript files and write to/read from it. 但基本上,您可以只在其中一个javascript文件中创建一个对象,然后对其进行写入/读取操作。

Something like 就像是

var data = { first: "first", second: "second" }

and just make as many variables in that object as you need. 并根据需要在该对象中创建尽可能多的变量。 And a function would access it like 函数会像

function(param) {
    var firstData = data.first;
}

Or I guess using cookies is closer to what you described as your own solution, but that seems like overkill if you just need to pass data between Javascript methods. 或者我猜想使用cookie更接近于您所描述的自己的解决方案,但是,如果您只需要在Javascript方法之间传递数据,那似乎就太过分了。

I opted for data attributes instead of above mentioned approaches and they are working fine for me. 我选择了数据属性,而不是上面提到的方法,它们对我来说很好用。 IE9 and IE8 are presenting some issues which I need to tackle. IE9和IE8提出了一些我需要解决的问题。 In IE9 & IE8 the data attributes work sometimes and sometimes not. 在IE9和IE8中,数据属性有时起作用,有时不起作用。 After I use the console to query data-attributes of the element, they start working. 在使用控制台查询元素的数据属性之后,它们开始工作。

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

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