How to Use Cookie & HTML5 localStorage

In a previous post, Jake shared a tutorial on building a step-by-step guide using Intro.js. One of our readers posed a question, asking how they can make the guide appear only once. In other words, once a user has completed the guide, the guide will not appear again in subsequent visits by the same user.

There are numerous ways to achieve this, but in this tutorial, we’ll just be using Cookie and localStorage. Briefly speaking, both Cookie and localStorage store some information in the browser, which can be used to determine whether to display the step-by-step guide or not.

Let’s get started, shall we?

Edit Web Content with HTML5 Contenteditable Attribute

Edit Web Content with HTML5 Contenteditable Attribute

One of the new features in HTML5 that attracted me is the native front-end editor. This feature is... Read more

Using Cookie

We will use the jQuery Cookie plugin to make the code look simpler. Download jQuery Cookie here, and put it in your HTML document along with jQuery, like so.

 <script src="jquery.js"></script>
 <script src="jquery.cookie.js"></script>
 

Then, we need to specify the name of our Cookie and also retrieve its value, which will be used later as reference when we run the IntroJs function.

In this example, we will name the Cookie IntroJS and store it in a variable called name while the value will be stored in a variable called value.

 var name = 'IntroJS';
 var value = $.cookie(name);
 

Next, using the .oncomplete() method that is provided in IntroJS, we can set a Cookie in the browser. Using the following code as an example, once the user has clicked the Done button on the tooltip, a Cookie named IntroJS will be created with the value of one.

 introJs().start().oncomplete(function() {
 $.cookie(name, 1);
 }
 

Now, refresh the browser and complete the the guide. If you are using Google Chrome, just go to View > Developer > Developer Tools > Resources (Tab) – you can see that a Cookie has been created (screenshot).

jquery cookies

As previously mentioned, we do not want the guide to appear again to users who have already completed the guide before. To do that, wrap the IntroJS function with a conditional function to run IntroJS function only if the Cookie named IntroJS is not set in the browser.

if (value == null) {
    introJs().start().oncomplete(function() {
        $.cookie(name, 1);
    });
}

However, the downside of using Cookie is that it will expire. Unless explicitly specified, the Cookie will become a Session Coookie, which means that it will be deleted when the user closes the browser.

Even if the expiration time is specified, this is still also not an ideal solution as the Cookie will still be removed eventually. So, let us now take a look at a second (better) solution, i.e. using localStorage.

Using HTML5 localStorage

Briefly speaking, localStorage works somewhat like a database; it stores some pieces of data – key and value – locally on the user’s browser, which then can be retrieved using JavaScript API. Unlike Cookie, localStorage is persistent.

The data will always be available inside the user’s browser, even after the user has closed the browser. localStorage also accepts more data than Cookies.

To get started, Let’s first set the key name and value. For your information, we use .getItem() to retrieve the value from a localStorage key.

 var name = 'IntroJS';
 var value = localStorage.getItem(name);
 

Similar to our first example with Cookie, we will run the IntroJS function only when there’s no specified data. In other words, once the data has been set, the guide should not appear. Using localStorage, we can set the data with .setItem(), like so.

if (value == null) {
    introJs().start().oncomplete(function() {
        localStorage.setItem(name, 1);
    });
};

Now, refresh your browser and complete the step-by-step guide. Then, go to View > Developer > Developer Tool > Resources (Tab) – in the LocalStorage section, you should find a new key and that its value has already been set.

local storage

Please note that localStorage is a relatively new technology – according to CanIUse.com, localStorage is only supported (at the time of this writing) in the following browsers: IE8+, Firefox 3.5+, Chrome 4.0+, Safari 4.0+, and Opera 10.5+.

If, for any reason, you need to cater to users with older browsers, you can use localStorage in conjunction with Cookies. Here is a sample code:

// Variable declaration
var name = 'IntroJS';

// Get value from local storage or cookie
var value = localStorage.getItem(name) || $.cookie(name);

// Function to set value in local storage or cookie
var func = function() {
  if (Modernizr.localstorage) { // If localstorage is supported
    localStorage.setItem(name, 1); // set the item in local storage
  } else { // If localstorage is not supported
    $.cookie(name, 1, { // set the item in cookie
      expires: 365 // cookie will expire after 365 days
    });
  }
};

// If value is null, start introJs and set value on completion or exit
if(value == null) {
  introJs().start().oncomplete(func).onexit(func);
}

This code uses Modernizr to detect the browser’s features. It will use localStorage if the browser supports it; otherwise, it will resort to using Cookies.

And that’s it, folks. We hope that you have found this tutorial helpful. For further reference, you can have a look at our previous tutorial on Modernizr.

WebsiteFacebookTwitterInstagramPinterestLinkedInGoogle+YoutubeRedditDribbbleBehanceGithubCodePenWhatsappEmail