I just built a jquery plugin to handle some data storages. When I try to read from storage, the result is the read item data.
But when I add
sessionStorage.setItem('test','some data');
or remove
sessionStorage.removeItem('test');
an item, I always get undefined
in the console.log
How can I get rid of the undefined?
What I tried was
var result = sessionStorage.setItem('test','some data');
though this would print it to a var but it doesn't.
My code:
(function ($) {
$.storage = {
add: function(name,value){
if(typeof value === 'object' )
{
/** convert object to json **/
value = JSON.stringify(value);
}
/** add to storage **/
sessionStorage.setItem(name,value);
},
read: function(name){
/** read from storage **/
var item = sessionStorage.getItem(name);
try {
item = $.parseJSON(item);
}catch(e){}
return item;
},
remove: function(name){
/** delete from storage **/
sessionStorage.removeItem(name);
},
clear: function(){
/** clear storage **/
sessionStorage.clear();
}
}
})(jQuery);
I just built a jquery plugin to handle some data storages. When I try to read from storage, the result is the read item data.
But when I add
sessionStorage.setItem('test','some data');
or remove
sessionStorage.removeItem('test');
an item, I always get undefined
in the console.log
How can I get rid of the undefined?
What I tried was
var result = sessionStorage.setItem('test','some data');
though this would print it to a var but it doesn't.
My code:
(function ($) {
$.storage = {
add: function(name,value){
if(typeof value === 'object' )
{
/** convert object to json **/
value = JSON.stringify(value);
}
/** add to storage **/
sessionStorage.setItem(name,value);
},
read: function(name){
/** read from storage **/
var item = sessionStorage.getItem(name);
try {
item = $.parseJSON(item);
}catch(e){}
return item;
},
remove: function(name){
/** delete from storage **/
sessionStorage.removeItem(name);
},
clear: function(){
/** clear storage **/
sessionStorage.clear();
}
}
})(jQuery);
Share
Improve this question
edited Oct 23, 2023 at 11:17
BenjyTec
11k4 gold badges23 silver badges50 bronze badges
asked May 7, 2014 at 9:55
DwzaDwza
6,5657 gold badges45 silver badges77 bronze badges
3
- "how can i get rid of the undefined, any ideas ???" — Why do you want to? – Quentin Commented May 7, 2014 at 10:15
- because it seams to be wrong... when i programm, run my application and undefined appears several times... – Dwza Commented May 7, 2014 at 10:16
-
@Dwza, I have create a fiddle jsfiddle/satpalsingh/mCNFT using your code. Can you reproduce your problem in fiddle. Also note
sessionStorage
is supported in IE8+ browser see caniuse./#search=sessionStorage – Satpal Commented May 7, 2014 at 10:58
2 Answers
Reset to default 3var result = sessionStorage.setItem('test','some data');
does not return anything.
sessionStorage.setItem('test','some data');
assigns value to 'test'
therefore the right hand side does not have any value.
What you want is
sessionStorage.setItem('test','some data');
var result = sessionStorage.getItem('test'); //<- here the right hand side is 'some data'
setItem
and removeItem
doesn't return any value. Thus you are getting undefined
. Also note removeItem
only accept one parameter.
Reference, Here is function sign for above methods
setItem(key:string, data:string):void
This public method stores data via specified key. Please note that if key was already stored, it will overwrite it. Another thing to consider is that if we store a key, and via sessionStorage.key(0) we obtain that key, then we store another key/data and finally we store again the first key, sessionStorage.key(0) will return the second key/data stored, and not the first one.
removeItem(key:string):void
This public method simply removes a key and related data from the storage.