Retrieving Last Key In Javascript Localstorage
I am doing a uni assignment so please excuse the coplete lack of knowledge here. I have been tasked with creating a web based app for a 'client' that requires me to open an invoice
Solution 1:
To display the last value of an array you should use values[values.length-1].
This will guarantee you get the absolute last one.
Solution 2:
simple put
var itemKey = localStorage.key(i);
var values = localStorage.getItem(itemKey); in a loop and increement i each time.
Solution 3:
You can try something like this
varlocalStorageKeys= Object.keys(localStorage);
getOrder(localStoragesKeys[localStoragesKeys.length-1]);
function getOrder(itemKey) {
varvalues= localStorage.getItem(itemKey);
values = values.split(";");
varname= values[0];
varcompany= values[1];
varcontactnumber= values[2];
varemail= values[3];
varaddress1= values[4];
varaddress2= values[5];
varsuburb= values[6]
varpostcode= values[7];
varcomments= values[8];
varbags= values[9];
vardistance= values[10];
varhdelivery_fee= values[11];
varhprice= values[12];
varhtotal_notax= values[13];
varhgst= values[14];
varhtotal_tax= values[15];
varhordernumber= values[16];
document.write('Name: ' + name + '<br />');
document.write('Company: ' + company + '<br />');
document.write('Contact: ' + contactnumber + '<br />');
document.write('Email; ' + email + '<br />');
document.write('Address; ' + address1 + '<br />');
document.write('Address; ' + address2 + '<br />');
document.write('Suburb; ' + suburb + '<br />');
document.write('Postcode; ' + postcode + '<br />');
document.write('Comments; ' + comments + '<br />');
document.write('Number of Bags; ' + bags + '<br />');
document.write('Distance; ' + distance + '<br />');
document.write('Delivery Fee; $' + hdelivery_fee + '<br />');
document.write('Price of Bags; $' + hprice + '<br />');
document.write('Total Ex-GST; $' + htotal_notax + '<br />');
document.write('GST; $' + hgst + '<br />');
document.write('Total Inc GST; $' + htotal_tax + '<br />');
document.write('hordernumber; ' + hordernumber + '<br />');
}
Solution 4:
If you must use localStorage
and cannot use any client side database then I would save each entry with a key like new Date().getTime()
with a known string in front of it, so that you have key-1368910344400
.
When you need to retrieve the last entry then you could do:
var keys = Object.keys(localStorage);
keys = keys.map(function(key) {
// check if the current key matches match "key-"// if that's the case create a new Date object with the ms obtained by a split// return the Date
});
// sort keys and take the most recent
This is usually a bad choice but as I understand it you're limited with choices, so you have to stretch things a bit.
If you could use a browser that supports html5 and webdb
then just throw localStorage
away and go for that :D, here is an example: http://www.html5rocks.com/en/tutorials/webdatabase/todo/
Post a Comment for "Retrieving Last Key In Javascript Localstorage"