首页 > 编程语言 > 详细

[Javascript] Web APIs: Persisting browser data with window.localStorage

时间:2016-01-27 22:42:40      阅读:179      评论:0      收藏:0      [点我收藏+]
Local Storage is a native JavaScript Web API that makes it easy to store and persist data (as key-value pairs) in the Browser. In this lesson, we‘ll walk through using window.localStorage to store feedback a user enters into a form (text) so that even if they close and re-open their browser, they won‘t loose their progress.
 
feedback.js
import inputStorage from ‘../input-storage/input-storage‘;

let feedback = {
    init() {
        inputStorage.restore(‘userFeedback‘, ‘.feedback__textarea‘);
        inputStorage.save(‘userFeedback‘, ‘.feedback__textarea‘);
    }
};

export default feedback;

 

inputStorage.js

let inputStorage = {
    restore(key, inputSelector) {
        if(localStorage[key]) {
            document.querySelector(inputSelector).value = localStorage[key];
        }
    },

    save(key, inputSelector) {
        let inputElement = document.querySelector(inputSelector);
        inputElement.addEventListener(‘input‘, () => {
            localStorage[key] = inputElement.value;
        });
    }
};

export default inputStorage;

 

[Javascript] Web APIs: Persisting browser data with window.localStorage

原文:http://www.cnblogs.com/Answer1215/p/5164660.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!