![](/img/trans.png)
[英]How to store data with localStorage using JavaScript and HTML?
[英]how to store and fetch data from array using localStorage in javascript
我需要的
然后获取数据。
refrence link
js代码
<script>
function favorite(sess_id,city,country,event_url)
{
// Save data to the current local store//
if (typeof(localStorage) == 'undefined' ) {
alert('Your browser does not support HTML5 localStorage. Try upgrading.');
}
else
{
try {
localStorage.setItem('id' ,sess_id);
}
catch (e)
{
if (e == QUOTA_EXCEEDED_ERR)
{
alert('Quota exceeded!'); //data wasn't successfully saved due to quota exceed so throw an error
}
}
try {
localStorage.setItem('city',city);
}
catch (e)
{
if (e == QUOTA_EXCEEDED_ERR)
{
alert('Quota exceeded!'); //data wasn't successfully saved due to quota exceed so throw an error
}
}
try {
localStorage.setItem('country',country);
}
catch (e)
{
if (e == QUOTA_EXCEEDED_ERR)
{
alert('Quota exceeded!'); //data wasn't successfully saved due to quota exceed so throw an error
}
}
try
{
localStorage.setItem('event_url',event_url);
}
catch (e)
{
if (e == QUOTA_EXCEEDED_ERR)
{
alert('Quota exceeded!'); //data wasn't successfully saved due to quota exceed so throw an error
}
}
}
/ *使用从本地存储中获取数据* /
var id= [];
var city = [];
var country =[];
var event_url= [];
// Retrieve the object from storage
var retrievedObject = localStorage.getItem('id');
console.log('id: ', JSON.parse(retrievedObject));
var city = localStorage.getItem('city');
console.log('city: ', JSON.parse(city));
var country = localStorage.getItem('country');
console.log('country: ', JSON.parse(country));
var event_url = localStorage.getItem('event_url');
console.log('event_url: ', JSON.parse(event_url));
}
问题
码
if (localStorage)
{
if (localStorage.length)
{
for (var i = 0; i < localStorage.length; i++)
{
console.log(localStorage.key(i));
id[i] = localStorage.getItem('id');
city[i] = localStorage.getItem('city');
country[i] = localStorage.getItem('country');
event_url[i] = localStorage.getItem('event_url');
console.log(id[i]);
console.log(city[i]);
console.log(country[i]);
console.log(event_url[i]);
}
}
else
{
alert('You have no favorite names stored');
}
}
使用数组和localStorage的基础非常简单。
要进行存储,请使用JSON.stringify()将您的JavaScript数组转换为JSON,然后只需使用一个localSTorage键即可设置该值。
当您需要访问数组时,请将其从localStorage中拉出,然后使用JSON.parse()将整个字符串转换回javascript数组。 提取数据后,请像使用其他阵列一样使用该阵列,然后在完成操作后将其存储回相同的localStorage密钥。
var existingArray=[
{id:1 , country:'Country1', url:'example.com'},
{id:2 , country:'Country2', url:'example2.com'}
];
localStorage.setItem('myEventsArray', JSON.stringify( existingArray));
/* on pageLoad */
var pageLoadArray = [];
var storedString = localStorage.getItem('myEventsArray');
if( storedString ){
pageLoadArray = JSON.parse(storedString);
if( pageLoadArray.length){
alert( pageLoadArray[0].country)
}
}
您从本地存储中检索数据的方式似乎错误。 我不明白,为什么要遍历本地存储长度。 相反,您可以执行此操作。
var id, city, country,; event_url;
// Retrieve the object from storage
var id = localStorage.getItem('id');
id = JSON.parse(id);
var city = localStorage.getItem('city');
city = JSON.parse(city);
var country = localStorage.getItem('country');
country = JSON.parse(country);
var event_url = localStorage.getItem('event_url');
event_url = JSON.parse(event_url);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.