javascript - If local storage empty then show message -
I have found the JavaScript code which stores birthdate data in local storage and displays data in a single device. Now do not show anything if the storage is empty. But I need to show a message if the local memory is empty; "Set your birthday first"
Here in the lower div display data
Thanks for everyone
& Lt; Title & gt; Untitled document & lt; / Title & gt; & Lt; Script type = "text / javascript" src = "http://code.jquery.com/jquery-1.3.2.js" & gt; & Lt; / Script & gt; & Lt; / Head & gt; & Lt; Body & gt; & Lt; Script & gt; $ (Function storedates (form) {var operation = "A"; // "a" = add; "e" = edit var selected_index = -1; // indicator of selected list item var tbClients = localStorage.getItem ("TbClients "); // Recover stored data tbClients = JSON.parse (tbClients); // if (tbClients == zero) // If there is no data, then start blank array tbClients = []; Add function () { Var: Client = JSON.stringify ({Birthday: $ ("# Birthday"). Val (), Patient: $ ("# Patient"). Val ()}); tbClients.push (Client); Local Storage .setItem ("TbClients", JSON.stringify (tbClients)); Edit the correct;} function Change the selected items at the local level (.) {TbClients [selected_index] = JSON.stringify ({ID: $ ("# name"). Val (),}); Storage .set item ("TBCLient", JSON.stringify (tbClients); Warning ("Data was edited.") Operation = "A"; // Return to default value return;} Delete function () {tbClients.splice (selected_index, 1); LocalStorage. Set ITEM ("TBCLent", JSON Stringies (TBCLent)); Warning ("Client deleted."); } Function list () {$ ("# tblList"). Html (""); $ ("# TblList") Html ("& lt; thead>" + "& lt; tr & gt;" + " " + "& lt; / tr & gt;); $ (" # patient number "). Attachment (cli.patientno);} $ (" #frmcadastre " Bind ("submit", function () {if (operation == "A") add return (); and edit return ();}); list (); $ (".btnEdit") Replace ("edit", "")); var cli = ("edit", ""); ("click", function () {operation = "E"; select_index = parseint ($ (this) JSON.parse (tbClients [selected_index]); $ ("#distribution day"). Val (c Li.ID);}); $ (".btn dealet"). Bind ("click", function () {replace_index = parseInt ($ (this) .attr ("alt"). Replace ("delete "," ")); Delete (); List ();});}); & lt; / script & gt; form name =" f1 "id =" frmcadastre "& gt; & lt; section Id = "align" & gt; input type = "text" id = "birth_day" name = "birth_day" placeholder = "birthday:" autocomplete = "off" tabindex = "2" class = "txtinput" Gt; & Lt; Br / & gt; & Lt; Br / & gt; & Lt; Input type = "text" id = "patient_no" name = "patient_no" placeholder = "not patient" auto-integer = "off" tabindex = "2" class = "text input" & gt; & Lt; Br / & gt; & Lt; Br / & gt; & Lt; Input type = "submit" name = "submit" id = "btn save" class = "submit" "tabindex =" 7 "onClick =" storeDates (this.form); "/>
< P> First, how to work through local storage if (! LocalStorage.tbClients) warning ("empty"); get data Another way to do this
if (! LocalStorage.getItem ("tbClients")) Alert ("Empty"); The difference between these methods :
- If such key-value does not exist, then you UNDEFINED value
- If the key key is not present you get value
Var message = localStorage.getItem ("SOME_KEY"). "First save your value to local storage"; console.log (message); // If the user already has something left in the local storage, we will see it, Otherwise "firstly save your value from local storage"
Comments
Post a Comment