douanrang4728 2017-06-29 17:49
浏览 82
已采纳

php js - 在他的计算机上存储用户设置

I would like to create a tiny script for web visitors :

A single sentence saying "Welcome {your name}." By cliking on {your name} the user would be able to write ... his name. I can do that by myself.

My question is : What is the best way for the user to see his name the next time he comes back ?

How to store this data in local user storage ?

Thanks for your advice !

  • 写回答

2条回答 默认 最新

  • dougou1127 2017-06-29 18:10
    关注

    If you want to store the name in the local storage of the user web browser you can use localStorage (just simple JavaScript). Something like this:

    html

    <input id='name'>
    <a onclick="saveName()">Save</a>
    <a onclick="showName()">Show Name</a>
    

    js

    function saveName() {
      var name = document.getElementById('name').value;
    
      if (typeof(Storage) !== "undefined" && name) {
        localStorage.setItem('userName', name);
      } else {
        alert('Sorry! No Web Storage support..');
      }
    }
    
    function showName() {
      if (typeof(Storage) !== "undefined") {
        alert(localStorage.getItem('userName'));
      } else {
        alert('Sorry! No Web Storage support..');
      }
    }
    

    If you want the user to be able to access his/her info independently of the browser you should consider storing it in the database.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料