For more information, please see:
Http://www.cnblogs.com/beiyuu/archive/2011/07/20/js-localstorage-userdata.html
The demo code involved is as follows:
<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 (t His.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>
There is a more practical technique to prevent the page from closing, display the close page Confirmation popup , the reference code is as follows:
Window.onbeforeunload = function () {if (!canleavepage ()) {return (' Confirm leaving the current page? Unsaved data will be lost! ‘);}};
JS Local Storage Information Implementation method (Localstorage and UserData)