JavaScript的localStorage返回结果是”null”。

huangapple go评论88阅读模式
英文:

localStorage of javascript gives me as a result "null"

问题

在使用以下命令后,

localStorage.setItem('message', 'hi'); 
console.log(localStorage.getItem('message'));

控制台显示null。我认为我执行了正确的命令。但它不起作用。我该如何修复它?

我尝试了很多事情,从语法规则开始,然后检查位置是否正确,还有单独的命令,但它总是返回null作为结果。我正在按照YouTube教程进行操作,他们的操作是正确的,但我却不行。

英文:

After using following commands,

  localStorage.setItem('message', 'hi'); 
  console.log(localStorage.getItem('message'));

the console says me null. I think I did the command right. But it does not work. How can I fix it?

I tried a lot of things starting off on the syntax rule, then I checked if the position was right and also the command by itself, but it always gave me null as a result, I'm following a youtube tutorial and they are getting it right but I don't.

答案1

得分: 1

Pietro!您提供的代码看起来正确,前提是...

  1. 您正在使用现代浏览器
  2. 该代码在浏览器控制台中依次执行
  3. 您已启用了Cookie

本地存储是一个涉及隐私的功能,因此如果您的浏览器禁用了Cookie,它可能无法正常工作。(例如,Safari通常会警告您正在请求的操作不安全,不过我不确定其他浏览器是否会有类似情况)。

如果您在任何其他情境中使用这些命令,请在此提供详细信息!例如,如果您使用的是旧版本浏览器,您可以提供版本号。如果您将这些函数作为网页脚本的一部分运行,而不是在控制台中运行,这也很重要。

希望这对您有所帮助!

英文:

Pietro! The code that you've provided looks correct, assuming that...

  1. You are using a modern browser
  2. The code is running, one command after another, in the browser console
  3. You have cookies enabled

Local Storage is a privacy-sensitive feature, so if your browser has cookies disabled, it might not be working correctly. (For instance, Safari will usually give you a warning that the operation you are requesting isn't secure, though I'm not sure about other browsers).

If you are using these commands in any other context, please provide them here! For instance, if you are using an older browser, you could provide the version number. If you are running these functions as part of a web script, instead of in the console, that will be important, too.

Hope this helps!

huangapple
  • 本文由 发表于 2023年6月8日 22:56:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76433158.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定