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条)

报告相同问题?

悬赏问题

  • ¥15 像这种代码要怎么跑起来?
  • ¥15 怎么改成循环输入删除(语言-c语言)
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误
  • ¥100 当AWR1843发送完设置的固定帧后,如何使其再发送第一次的帧
  • ¥15 图示五个参数的模型校正是用什么方法做出来的。如何建立其他模型
  • ¥100 描述一下元器件的基本功能,pcba板的基本原理
  • ¥15 STM32无法向设备写入固件