javascript - Update JS object based on local storeage -
i'm trying create setup object app url's in of url values dynamic , stored in local storage. question how update setup object based on latest local storage?
html:
<a href="javascript:slideonlyone('addon1')">addon 1</a> <a href="javascript:slideonlyone('addon2')">addon 2</a> <a href="javascript:slideonlyone('addon3')">addon 3</a>
javascript:
window.localstorage.setitem('tmpadd','default'); var appurl = { 'jsonurl1' : 'http://myurl.com/' + window.localstorage.getitem('tmpadd'), 'jsonurl2' : 'http://myurl.com/category/' + window.localstorage.getitem('tmpadd') }; function slideonlyone(item) { window.localstorage.setitem('tmpadd', item ); console.log(window.localstorage.getitem('tmpadd'),appurl.jsonurl1); }
see working example here:
update urls whenever change local storage values:
var appurl = { }; updateurls('default'); function updateurls(tmpadd) { window.localstorage.setitem('tmpadd', tmpadd); appurl.jsonurl1 = 'http://myurl.com/' + tmpadd; appurl.jsonurl1 = 'http://myurl.com/category/' + tmpadd; } function slideonlyone(item) { updateurls(item); console.log(window.localstorage.getitem('tmpadd')); console.log(appurl.jsonurl1); }
another way turn jsonurl1
, jsonurl2
functions @ current value of localstorage.tmpadd
, create url appropriately. make functions ordinary properties using custom property gettersmdn, overkill.
var appurl = { 'jsonurl1' : function() { return 'http://myurl.com/' + window.localstorage.getitem('tmpadd'); }, 'jsonurl2' : function() { return 'http://myurl.com/category/' + window.localstorage.getitem('tmpadd'); } }; // usage: replace appurl.jsonurl1 appurl.jsonurl1()
Comments
Post a Comment