I was trying to retrieve all the all the localStorage.setItem() that I set but I was not successful at this time.
This is my code
var base_url = window.location.origin;
$.ajax({
type: 'POST',
url: base_url + '/demo/now',
dataType: 'json',
success: function(data) {
localStorage.setItem('visitor_info', JSON.stringify(data.visitor_info));
localStorage.setItem('visitor_country', data.visitor_country);
localStorage.setItem('phone2', data.phone2);
localStorage.setItem('countryUrl', data.countryUrl);
}
});
right now I'm successful in saving it to localStorage, but I don't know how to retrieve all this.
This is my code for retrieving it.
function localStorageGetItem() {
localStorage.getItem('visitor_country');
localStorage.getItem('phone2');
localStorage.getItem('countryUrl');
return "blank so far"
}
I know I need to put value in return but I'm not sure how to return all these 3 getItem.
and this is where I need to call the localStorageGetItem()
$(document).ready(function() {
localStorageGetItem();
}
Thank you.
If I'm reading this correctly, it sounds like you simply want to return an object with properties matching some of the keys stored in localStorage
.
function localStorageGetItem() {
return {
visitor_country: localStorage.getItem('visitor_country'),
phone2: localStorage.getItem('phone2'),
countryUrl: localStorage.getItem('countryUrl')
}
}
You can then use it like this...
var localData = localStorageGetItem();
console.log(localData.visitor_country); // etc
Keep in mind, each of these properties will be null
if you call this before the data is stored via localStorage.setItem
.
I know I need to put value in return but I'm not sure how to return all these 3 getItem.
You can assign these into a new object or fetch them via localStorage as you did, since its a global property.
But the problem why you cann't revieve them is answered by Jaromanda X
var base_url = window.location.origin;
var myStorage = {}
$.when($.ajax({
type: 'POST',
url: base_url + '/demo/now',
dataType: 'json',
success: function(data) {
localStorage.setItem('visitor_info', JSON.stringify(data.visitor_info));
localStorage.setItem('visitor_country', data.visitor_country);
localStorage.setItem('phone2', data.phone2);
localStorage.setItem('countryUrl', data.countryUrl);
myStorage = localStorage
}
})).then(console.log(myStorage));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.