I am unable to use local storage for javascript. This is my first time trying local storage. I have two seperate HTML docs each with their own set of javascript code.
Doc 1 Javascript:
<script>
function callData() {
document.getElementById("1").innerHTML = localStorage.getItem("name1");
document.getElementById("2").innerHTML = localStorage.getItem("name2");
document.getElementById("3").innerHTML = localStorage.getItem("name3");
setNames();
}
function setNames() {
var 1st = document.getElementById("1").innerHTML;
var 2nd = document.getElementById("2").innerHTML;
var 3rd = document.getElementById("3").innerHTML;
if (typeof(Storage) != "undefined") {
localStorage.setItem("1", 1st);
localStorage.setItem("2", 2nd);
localStorage.setItem("3", 3rd);
}
else {
alert("Sorry, your browser does not support Web Storage"); }
}
</script>
Doc 1 Html
<body onLoad = "callData()">
<a style="text-align:left;" href= cameraApp.html>New Contact</a>
<h1 style="background-color:black; color:white; font-family:helvettica;">Address Book</h1>
<h3 style="background-color:blue; color:white; font-family:helvettica;">Contacts</h3>
<a id=1></a>
<a id=2></a>
<a id=3></a>
</body
Doc 2 JavaScript
<script>
function done() {
var 1st = localStorage.getItem("1");
var 2nd = localStorage.getItem("2");
var 3rd = localStorage.getItem("3");
var name = document.getElementById('nameTextfield').value;
if(1st = "") {
localStorage.setItem("name1",name);
alert("Success, Contact created");
} else if(2nd = "") {
localStorage.setItem("name2",name);
alert("Success, Contact created");
} else if(3rd = "") {
localStorage.setItem("name3",name);
alert("Success, Contact created");
} else {
alert("Sorry, Maximum contact limit reached");
}
}
</script>
</head>
Doc 2 HTML
<body>
<a style="text-align:left;" href= phonebook.html>Back</a>
<h1 style="background-color:black; color:white; font-family:helvettica;">Create a Contact</h1><br>
<form name="info">
<p id="name">Name: </p><input id="nameTextfield" type="textfield"/><br>
</form>
<button onclick="done()">Done</button>
</form>
</body>
The Doc 2 is meant to store the name given by the user and pass it to the Local Storage. Doc 1 is then supposed to display the names that have been stored in the local storage. Whenever I try to use this, after clicking the done button on doc 2 the page just refreshes and when I go back to doc 1 no names are added. I am using the Android SDK for the coding. I have tried using local storage for a hello world document so I know that it works for SDK.
Variable names can't start with numbers ( Correct me if i'm wrong ).
Which is throwing error
Uncaught SyntaxError: Unexpected token ILLEGAL.
Also, you're checking whether the value exists or not by comparing to empty string ( ""
), which will not also work,
You can check it instead like
if(!variable){
//variable does not exist
}
fixing these seems to solve the issue
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.