File session.js
/** * Implements cookie-less JavaScript session variables * v1.0 * * By Craig Buckler, Optimalworks.net * * As featured on SitePoint.com * Please use as you wish at your own risk. * * Usage: * * // store a session value/object * Session.set(name, object); * * // retreive a session value/object * Session.get(name); * * // clear all session data * Session.clear(); * * // dump session data * Session.dump(); */ if (JSON && JSON.stringify && JSON.parse) var Session = Session || (function() { // window object var win = window.top || window; // session store var store = (win.name ? JSON.parse(win.name) : {}); // save store on page unload function Save() { win.name = JSON.stringify(store); }; // page unload event if (window.addEventListener) window.addEventListener("unload", Save, false); else if (window.attachEvent) window.attachEvent("onunload", Save); else window.onunload = Save; // public methods return { // set a session variable set: function(name, value) { store[name] = value; }, // get a session value get: function(name) { return (store[name] ? store[name] : undefined); }, // clear session clear: function() { store = {}; }, // dump session data dump: function() { return JSON.stringify(store); } }; })();
========================================
File json-serial.js
/** * Implements JSON stringify and parse functions * v1.0 * * By Craig Buckler, Optimalworks.net * * As featured on SitePoint.com * Please use as you wish at your own risk. * * Usage: * * // serialize a JavaScript object to a JSON string * var str = JSON.stringify(object); * * // de-serialize a JSON string to a JavaScript object * var obj = JSON.parse(str); */ var JSON = JSON || {}; // implement JSON.stringify serialization JSON.stringify = JSON.stringify || function (obj) { var t = typeof (obj); if (t != "object" || obj === null) { // simple data type if (t == "string") obj = '"'+obj+'"'; return String(obj); } else { // recurse array or object var n, v, json = [], arr = (obj && obj.constructor == Array); for (n in obj) { v = obj[n]; t = typeof(v); if (t == "string") v = '"'+v+'"'; else if (t == "object" && v !== null) v = JSON.stringify(v); json.push((arr ? "" : '"' + n + '":') + String(v)); } return (arr ? "[" : "{") + String(json) + (arr ? "]" : "}"); } }; // implement JSON.parse de-serialization JSON.parse = JSON.parse || function (str) { if (str === "") str = '""'; eval("var p=" + str + ";"); return p; };
=========================
var firstTime = Session.get("firstTime") || { first: true }; if (firstTime.first == true) { console.log("lan dau"); Session.set("firstTime", {first: false}); } else { console.log("lan n"); Session.clear(); }