JS本機存放區資訊的實現方式(localStorage 與 userData)

來源:互聯網
上載者:User

標籤:pen   資訊   date   dom   isp   text   ack   本機存放區   his   

詳細介紹請看:

http://www.cnblogs.com/beiyuu/archive/2011/07/20/js-localstorage-userdata.html

 

裡面涉及到的 demo 代碼如下:

<script type="text/javascript">(function() {window.localData = {hname : location.hostname ? location.hostname : ‘localStatus‘,isLocalStorage : window.localStorage ? true : false,dataDom : null,initDom : function() {if (!this.dataDom) {try {this.dataDom = document.createElement(‘input‘);this.dataDom.type = ‘hidden‘;this.dataDom.style.display = "none";this.dataDom.addBehavior(‘#default#userData‘);document.body.appendChild(this.dataDom);var exDate = new Date();exDate = exDate.getDate() + 30;this.dataDom.expires = exDate.toUTCString();} catch (ex) {return false;}}return true;},set : function(key, value) {if (this.isLocalStorage) {window.localStorage.setItem(key, value);} else {if (this.initDom()) {this.dataDom.load(this.hname);this.dataDom.setAttribute(key, value);this.dataDom.save(this.hname)}}},get : function(key) {if (this.isLocalStorage) {return window.localStorage.getItem(key);} else {if (this.initDom()) {this.dataDom.load(this.hname);return this.dataDom.getAttribute(key);}}},remove : function(key) {if (this.isLocalStorage) {localStorage.removeItem(key);} else {if (this.initDom()) {this.dataDom.load(this.hname);this.dataDom.removeAttribute(key);this.dataDom.save(this.hname)}}}};var text = document.getElementById(‘localDataHook‘);var btn = document.getElementById(‘clearBtnHook‘);window.onbeforeunload = function() {localData.set(‘beiyuuData‘, text.value);};btn.onclick = function() {localData.remove(‘beiyuuData‘);text.value = ‘‘};if (localData.get(‘beiyuuData‘)) {text.value = localData.get(‘beiyuuData‘);}})();</script>

 

還有一個比較實用的技術,阻止頁面關閉,顯示 關閉頁面確認彈出框,參考代碼如下:

window.onbeforeunload = function() {if (!canLeavePage()) {return (‘確認離開當前頁面嗎?未儲存的資料將會丟失!‘);}};

JS本機存放區資訊的實現方式(localStorage 與 userData)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.