Posted injQuery / JavaScript

jQuery Save and retrieve Session Variable Value

jQuery Save and retrieve Session Variable Value

In this Post We Will Explain About is jQuery Save and retrieve Session Variable Value With Example and Demo.Welcome on Pakainfo.com – Examples, The best For Learn web development Tutorials,Demo with Example! Hi Dear Friends here u can know to jQuery Session-How to use session in jQueryExample

In this post we will show you Best way to implement jquery session variable Example, hear for get session value in javascript stepswith Download .we will give you demo,Source Code and examples for implement Step By Step Good Luck!.

jquery.session.js

(function($){
 
    $.session = {
 
        _id: null,  //set ID
        _cookieCache: undefined,//set _cookieCache
        _init: function() //set _init function
        {
            if (!window.name) {
                window.name = Math.random();
            }
            this._id = window.name;
            this._initCache();
 
            var data_matches = (new RegExp(this._generatePrefix() + "=([^;]+);")).exec(document.cookie);
            if (data_matches && document.location.protocol !== data_matches[1]) {
			//this means init value
               this._clearSession();
			   //for loop
               for (var user_key in this._cookieCache) {
				//simple try and catch block
                   try {
                   window.sessionStorage.setItem(user_key, this._cookieCache[user_key]);
                   } catch (e) {};
               }
            }
 
			// here cookie expires date
            document.cookie = this._generatePrefix() + "=" + document.location.protocol + ';path=/;expires=' + (new Date((new Date).getTime() + 120000)).toUTCString(); //set date
 
        },
 
        _generatePrefix: function() //call _generatePrefix function
        {
			//result return here
            return '__session:' + this._id + ':';
        },
 
        _initCache: function() //call _initCache function
        {
            var cookies = document.cookie.split(';');
			//this means init value
            this._cookieCache = {};
            for (var i in cookies) {
                var liveCode = cookies[i].split('=');
				//result return here
                if ((new RegExp(this._generatePrefix() + '.+')).test(liveCode[0]) && liveCode[1]) {
                    this._cookieCache[liveCode[0].split(':', 3)[2]] = liveCode[1];
                }
            }
        },
 
        _setFallback: function(user_key, value, onceOnly) //call _setFallback function
        {
            var cookie = this._generatePrefix() + user_key + "=" + value + "; path=/";
            if (onceOnly) {
                cookie += "; expires=" + (new Date(Date.now() + 120000)).toUTCString();
            }
            document.cookie = cookie;
			//this means init value
            this._cookieCache[user_key] = value;
			//result return here
            return this;
        },
 
        _getFallback: function(user_key) //call _getFallback function
        {
		//this means init value
            if (!this._cookieCache) {
                this._initCache();
            }
			//result return here
            return this._cookieCache[user_key]; 
        },
 
        _clearFallback: function() //call _clearFallback function
        {
            for (var i in this._cookieCache) {
				//for loop
                document.cookie = this._generatePrefix() + i + '=; path=/; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
            }
			//result return here
            this._cookieCache = {};
        },
 
        _deleteFallback: function(user_key) //call _deleteFallback function
        {
				//result return here
            document.cookie = this._generatePrefix() + user_key + '=; path=/; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
			//this means init value
            delete this._cookieCache[user_key];
        },
 
        get: function(user_key) //call get function
        {
			//result return here
            return window.sessionStorage.getItem(user_key) || this._getFallback(user_key);
        },
 
        set: function(user_key, value, onceOnly) //call set function
        {
            try {
                window.sessionStorage.setItem(user_key, value);
            } catch (e) {}
			//this means init value
            this._setFallback(user_key, value, onceOnly || false);
			//result return here
            return this;
        },
        
        'delete': function(user_key){  //call delete function
		//result return here
            return this.remove(user_key);
        },
 
        remove: function(user_key) //call remove function
        {
            try {
            window.sessionStorage.removeItem(user_key);
            } catch (e) {};
			//this means init value
            this._deleteFallback(user_key);
			//result return here
            return this;
        },
 
        _clearSession: function() //call _clearSession function
        {
          try {
                window.sessionStorage.clear();
            } catch (e) {
				//for loop
                for (var i in window.sessionStorage) {
                    window.sessionStorage.removeItem(i);
                }
            }
			//result return here
        },
 
        clear: function() //call clear function
        {
			//this means init value
            this._clearSession();
            this._clearFallback();
			//result return here
            return this;
        }
 
    };
 
    $.session._init();
 
})(jQuery);

How to access session inside Jquery Example

//simple session get using user_key
$.session.get('user_key');
Output => "value"

// Clear all simple value in session 
$.session.clear(); 
 
//simple session get using user_key "undefined" 
$.session.get('user_key');  
After Clear output => undefined
 
 // Set a simple data value and call in like single line 
$.session.set('user_key', 'value').get('user_key');
output => "value"

// Remove all the data particular user_key from session 
$.session.remove('user_key'); 
 
//key get to data 
$.session.get('some user_key');
After Remove output => undefined

Example

I hope you have Got What is Save and retrieve Session data via Ajax using JQuery And how it works.I would Like to have FeadBack From My Blog(Pakainfo.com) readers.Your Valuable FeadBack,Any Question,or any Comments abaout This Article(Pakainfo.com) Are Most Always Welcome.

I am Jaydeep Gondaliya , a software engineer, the founder and the person running Pakainfo. I'm a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, JavaScript, jQuery, Laravel, CodeIgniter, VueJS, AngularJS and Bootstrap from the early stage.

Leave a Reply

Your email address will not be published. Required fields are marked *

We accept paid guest Posting on our Site : Guest Post Chat with Us On Skype