11

I am using following code to store value in session object in jQuery but i get error $.session function is not a function.

I also add plugin "jquery.session.js" from githhub site but it is not work. Pls let me help out what's wrong

 // To Store
 $(function() {
      $.session("myVar", "value");
 });


 // To Read
$(function() {
      alert( $.session("myVar") );
});

If there is any other way to do this...then also tell me.....

Imaky
  • 1,227
  • 1
  • 16
  • 36
user1206218
  • 213
  • 2
  • 5
  • 10
  • 1
    Did you download and include the session plugin? https://github.com/AlexChittock/JQuery-Session-Plugin – Dutchie432 Jul 20 '12 at 13:22
  • not by jquery , but if you want using javascript then consider this question- http://stackoverflow.com/questions/2257631/how-create-a-session-using-javascript – vivex Mar 24 '14 at 15:17

2 Answers2

19

Assuming you're referring to this plugin, your code should be:

// To Store
$(function() {
    $.session.set("myVar", "value");
});


// To Read
$(function() {
    alert($.session.get("myVar"));
});

Before using a plugin, remember to read its documentation in order to learn how to use it. In this case, an usage example can be found in the README.markdown file, which is displayed on the project page.

Frédéric Hamidi
  • 258,201
  • 41
  • 486
  • 479
  • Just on a side note, if you intend to use this to store security-sensitive information, since these sessions are handled on the client-side, it could be vulnerable to editing. – Dutchie432 Jul 20 '12 at 13:24
  • Hi Frederic, I have used above function which you suggest me. I also use the plugin as you thought but i get error like this.............. Operation is not supported [Break On This Error] Operation is not supported return window.sessionStorage.getItem(key) || this._getFallback(key); jquery.session.js (line 85) – user1206218 Jul 21 '12 at 06:00
  • @user, looks like your browser does not support session storage. Does it pass [this test](http://dev-test.nemikor.com/web-storage/support-test/)? – Frédéric Hamidi Jul 21 '12 at 06:12
13

In my opinion you should not load and use plugins you don't have to. This particular jQuery plugin doesn't give you anything since directly using the JavaScript sessionStorage object is exactly the same level of complexity. Nor, does the plugin provide some easier way to interact with other jQuery functionality. In addition the practice of using a plugin discourages a deep understanding of how something works. sessionStorage should be used only if its understood. If its understood, then using the jQuery plugin is actually MORE effort.

Consider using sessionStorage directly: https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Storage#sessionStorage

Shog9
  • 156,901
  • 35
  • 231
  • 235
ktamlyn
  • 4,519
  • 2
  • 30
  • 41
  • 2
    The plugin uses cookies automatically, as a fallback, if html5 session storage is not available. – Julian Mar 21 '13 at 03:07
  • 1
    That is true, but no where is this documented. Also cookies and session variables are very different things. If the goal is to account for all circumstances, then one should use something like this http://samy.pl/evercookie/ – ktamlyn Apr 14 '13 at 14:22