JS Local Storage Information Implementation method (Localstorage and UserData)

Source: Internet
Author: User
Tags close page

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.