简体   繁体   English

javascript本地存储不起作用

[英]javascript Local Storage not working

I am unable to use local storage for javascript. 我无法将本地存储用于javascript。 This is my first time trying local storage. 这是我第一次尝试本地存储。 I have two seperate HTML docs each with their own set of javascript code. 我有两个单独的HTML文档,每个文档都有自己的javascript代码集。

Doc 1 Javascript: 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 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 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 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 2旨在存储用户给定的名称,并将其传递给本地存储。 Doc 1 is then supposed to display the names that have been stored in the local storage. 然后,文档1应该显示已存储在本地存储中的名称。 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. 每当我尝试使用它时,单击doc 2上的完成按钮后,页面就会刷新,而当我返回doc 1时,不会添加任何名称。 I am using the Android SDK for the coding. 我正在使用Android SDK进行编码。 I have tried using local storage for a hello world document so I know that it works for SDK. 我已经尝试将本地存储用于hello world文档,所以我知道它适用于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 修复这些似乎可以解决问题

Doc1 文档1

Doc2 文档2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM