• 18-19 College Green, Dublin 2
  • 01 685 9088
  • info@cunninghamwebsolutions.com
  • cunninghamwebsolutions
    Cunningham Web Solutions
    • Home
    • About Us
    • Our Services
      • Web Design
      • Digital Marketing
      • SEO Services
      • E-commerce Websites
      • Website Redevelopment
      • Social Media Services
    • Digital Marketing
      • Adwords
      • Social Media Services
      • Email Marketing
      • Display Advertising
      • Remarketing
    • Portfolio
    • FAQ’s
    • Blog
    • Contact Us
    MENU CLOSE back  

    Creating A Shopping Cart With HTML5 Web Storage

    You are here:
    1. Home
    2. Web Design
    3. Creating A Shopping Cart With HTML5 Web Storage
    Thumbnail for 23553
    Smashing Editorial

    Creating A Shopping Cart With HTML5 Web Storage

    Creating A Shopping Cart With HTML5 Web Storage

    Matt Zand

    2019-08-26T14:30:59+02:00
    2019-08-27T12:30:20+00:00

    With the advent of HTML5, many sites were able to replace JavaScript plugin and codes with simple more efficient HTML codes such as audio, video, geolocation, etc. HTML5 tags made the job of developers much easier while enhancing page load time and site performance. In particular, HTML5 web storage was a game changer as they allow users’ browsers to store user data without using a server. So the creation of web storage, allowed front-end developers to accomplish more on their website without knowing or using server-side coding or database.

    Online e-commerce websites predominantly use server-side languages such as PHP to store users’ data and pass them from one page to another. Using JavaScript back-end frameworks such as Node.js, we can achieve the same goal. However, in this tutorial, we’ll show you step by step how to build a shopping cart with HTML5 and some minor JavaScript code. Other uses of the techniques in this tutorial would be to store user preferences, the user’s favorite content, wish lists, and user settings like name and password on websites and native mobile apps without using a database.

    Many high-traffic websites rely on complex techniques such as server clustering, DNS load balancers, client-side and server-side caching, distributed databases, and microservices to optimize performance and availability. Indeed, the major challenge for dynamic websites is to fetch data from a database and use a server-side language such as PHP to process them. However, remote database storage should be used only for essential website content, such as articles and user credentials. Features such as user preferences can be stored in the user’s browser, similar to cookies. Likewise, when you build a native mobile app, you can use HTML5 web storage in conjunction with a local database to increase the speed of your app. Thus, as front-end developers, we need to explore ways in which we can exploit the power of HTML5 web storage in our applications in the early stages of development.

    I have been a part of a team developing a large-scale social website, and we used HTML5 web storage heavily. For instance, when a user logs in, we store the hashed user ID in an HTML5 session and use it to authenticate the user on protected pages. We also use this feature to store all new push notifications — such as new chat messages, website messages, and new feeds — and pass them from one page to another. When a social website gets high traffic, total reliance on the server for load balancing might not work, so you have to identify tasks and data that can be handled by the user’s browser instead of your servers.

    Project Background

    A shopping cart allows a website’s visitor to view product pages and add items to their basket. The visitor can review all of their items and update their basket (such as to add or remove items). To achieve this, the website needs to store the visitor’s data and pass them from one page to another, until the visitor goes to the checkout page and makes a purchase. Storing data can be done via a server-side language or a client-side one. With a server-side language, the server bears the weight of the data storage, whereas with a client-side language, the visitor’s computer (desktop, tablet or smartphone) stores and processes the data. Each approach has its pros and cons. In this tutorial, we’ll focus on a simple client-side approach, based on HTML5 and JavaScript.

    Note: In order to be able to follow this tutorial, basic knowledge of HTML5, CSS and JavaScript is required.

    Project Files

    Click here to download the project’s source files. You can see a live demo, too.

    Overview Of HTML5 Web Storage

    HTML5 web storage allows web applications to store values locally in the browser that can survive the browser session, just like cookies. Unlike cookies that need to be sent with every HTTP request, web storage data is never transferred to the server; thus, web storage outperforms cookies in web performance. Furthermore, cookies allow you to store only 4 KB of data per domain, whereas web storage allows at least 5 MB per domain. Web storage works like a simple array, mapping keys to values, and they have two types:

    • Session storage
      This stores data in one browser session, where it becomes available until the browser or browser tab is closed. Popup windows opened from the same window can see session storage, and so can iframes inside the same window. However, multiple windows from the same origin (URL) cannot see each other’s session storage.
    • Local storage
      This stores data in the web browser with no expiration date. The data is available to all windows with the same origin (domain), even when the browser or browser tabs are reopened or closed.

    Both storage types are currently supported in all major web browsers. Keep in mind that you cannot pass storage data from one browser to another, even if both browsers are visiting the same domain.

    Build A Basic Shopping Cart

    To build our shopping cart, we first create an HTML page with a simple cart to show items, and a simple form to add or edit the basket. Then, we add HTML web storage to it, followed by JavaScript coding. Although we are using HTML5 local storage tags, all steps are identical to those of HTML5 session storage and can be applied to HTML5 session storage tags. Lastly, we’ll go over some jQuery code, as an alternative to JavaScript code, for those interested in using jQuery.

    Add HTML5 Local Storage To Shopping Cart

    Our HTML page is a basic page, with tags for external JavaScript and CSS referenced in the head.

    <!DOCTYPE HTML>
    <html lang="en-US">
    <head>
    <title>HTML5 Local Storage Project</title>
    <META charset="UTF-8">
    <META name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <META NAME='rating' CONTENT='General' />
    <META NAME='expires' CONTENT='never' />
    <META NAME='language' CONTENT='English, EN' />
    <META name="description" content="shopping cart project with HTML5 and JavaScript">
    <META name="keywords" content="HTML5,CSS,JavaScript, html5 session storage, html5 local storage">
    <META name="author" content="dcwebmakers.com">
    <script src="Storage.js"></script>
    <link rel="stylesheet" href="StorageStyle.css">
    </head>
    

    Below is the HTML content for the page’s body:

    <form name=ShoppingList>
    
            <div id="main">
                <table>
                    <tr>
    
                <td><b>Item:</b><input type=text name=name></td>
                <td><b>Quantity:</b><input type=text name=data></td>
    
                    </tr>
    
                    <tr>
                        <td>
             <input type=button value="Save"   onclick="SaveItem()"> 
            <input type=button value="Update" onclick="ModifyItem()"> 
            <input type=button value="Delete" onclick="RemoveItem()">
                        </td>
                    </tr>
                </table>
            </div>
    
            <div id="items_table">
                <h3>Shopping List</h3>
                <table id=list></table>
                <p>
            <label><input type=button value="Clear" onclick="ClearAll()">
                    <i>* Delete all items</i></label>
                </p>
            </div>
        </form>
    

    Adding JavaScript To The Shopping Cart

    We’ll create and call the JavaScript function doShowAll() in the onload() event to check for browser support and to dynamically create the table that shows the storage name-value pair.

    <body onload="doShowAll()">
    

    Alternatively, you can use the JavaScript onload event by adding this to the JavaScript code:

    window.load=doShowAll();
    

    Or use this for jQuery:

    $( window ).load(function() {
      doShowAll();
    });
    

    In the CheckBrowser() function, we would like to check whether the browser supports HTML5 storage. Note that this step might not be required because most modern web browsers support it.

    /*
    =====> Checking browser support.
     //This step might not be required because most modern browsers do support HTML5.
     */
     //Function below might be redundant.
    function CheckBrowser() {
        if ('localStorage' in window && window['localStorage'] !== null) {
            // We can use localStorage object to store data.
            return true;
        } else {
                return false;
        }
    }
    

    Inside the doShowAll(), if the CheckBrowser() function evaluates first for browser support, then it will dynamically create the table for the shopping list during page load. You can iterate the keys (property names) of the key-value pairs stored in local storage inside a JavaScript loop, as shown below. Based on the storage value, this method populates the table dynamically to show the key-value pair stored in local storage.

    // Dynamically populate the table with shopping list items.
    //Step below can be done via PHP and AJAX, too. 
    function doShowAll() {
        if (CheckBrowser()) {
            var key = "";
            var list = "<tr><th>Item</th><th>Value</th></tr>n";
            var i = 0;
            //For a more advanced feature, you can set a cap on max items in the cart.
            for (i = 0; i <= localStorage.length-1; i++) {
                key = localStorage.key(i);
                list += "<tr><td>" + key + "</td>n<td>"
                        + localStorage.getItem(key) + "</td></tr>n";
            }
            //If no item exists in the cart.
            if (list == "<tr><th>Item</th><th>Value</th></tr>n") {
                list += "<tr><td><i>empty</i></td>n<td><i>empty</i></td></tr>n";
            }
            //Bind the data to HTML table.
            //You can use jQuery, too.
            document.getElementById('list').innerHTML = list;
        } else {
            alert('Cannot save shopping list as your browser does not support HTML 5');
        }
    }
    

    Note: Either you or your framework will have a preferred method of creating new DOM nodes. To keep things clear and focused, our example uses .innerHTML even though we’d normally avoid that in production code.

    Tip: If you’d like to use jQuery to bind data, you can just replace document.getElementById('list').innerHTML = list; with $(‘#list').html()=list;.

    Run And Test The Shopping Cart

    In the previous two sections, we added code to the HTML head, and we added HTML to the shopping cart form and basket. We also created a JavaScript function to check for browser support and to populate the basket with the items in the cart. In populating the basket items, the JavaScript fetches values from HTML web storage, instead of a database. In this part, we’ll show you how the data are inserted into the HTML5 storage engine. That is, we’ll use HTML5 local storage in conjunction with JavaScript to insert new items to the shopping cart, as well as edit an existing item in the cart.

    Note: I’ve added tips sections below to show jQuery code, as an alternative to the JavaScript ones.

    We’ll create a separate HTML div element to capture user input and submission. We’ll attach the corresponding JavaScript function in the onClick event for the buttons.

    <input type="button" value="Save"   onclick="SaveItem()">
    <input type="button" value="Update" onclick="ModifyItem()"> 
    <input type="button" value="Delete" onclick="RemoveItem()">
    

    You can set properties on the localStorage object similar to a normal JavaScript object. Here is an example of how we can set the local storage property myProperty to the value myValue:

    localStorage.myProperty="myValue";
    

    You can delete local storage property like this:

    delete localStorage.myProperty;
    

    Alternately, you can use the following methods to access local storage:

    localStorage.setItem('propertyName','value'); 
    localStorage.getItem('propertyName'); 
    localStorage.removeItem('propertyName');
    

    To save the key-value pair, get the value of the corresponding JavaScript object and call the setItem method:

    function SaveItem() {
             
        var name = document.forms.ShoppingList.name.value;
        var data = document.forms.ShoppingList.data.value;
        localStorage.setItem(name, data);
        doShowAll();
        
    }
    

    Below is the jQuery alternative for the SaveItem function. First, add an ID to the form inputs:

    <td><b>Item:</b><input type=text name="name" id="name"></td>
    <td><b>Quantity:</b><input type=text name="data" id="data"></td>
    

    Then, select the form inputs by ID, and get their values. As you can see below, it is much simpler than JavaScript:

    function SaveItem() {         
        var name = $("#name").val();
        var data = $("#data").val();
        localStorage.setItem(name, data);
        doShowAll();    
    }
    

    To update an item in the shopping cart, you have to first check whether that item’s key already exists in local storage, and then update its value, as shown below:

    //Change an existing key-value in HTML5 storage.
    function ModifyItem() {
        var name1 = document.forms.ShoppingList.name.value;
        var data1 = document.forms.ShoppingList.data.value;
        //check if name1 is already exists
        
    //Check if key exists.
                if (localStorage.getItem(name1) !=null)
                {
                  //update
                  localStorage.setItem(name1,data1);
                  document.forms.ShoppingList.data.value = localStorage.getItem(name1);
                }
            
        
        doShowAll();
    }
    

    Below shows the jQuery alternative.

    function ModifyItem() {
        var name1 = $("#name").val();
        var data1 = $("#data").val();
        //Check if name already exists.
    //Check if key exists.
             if (localStorage.getItem(name1) !=null)
             {
               //update
               localStorage.setItem(name1,data1);
               var new_info=localStorage.getItem(name1);
               $("#data").val(new_info);
             }       
        
        doShowAll();
    }
    

    We will use the removeItem method to delete an item from storage.

    function RemoveItem() 
    { 
    var name=document.forms.ShoppingList.name.value;
    document.forms.ShoppingList.data.value=localStorage.removeItem(name); 
    doShowAll();
    }
    

    Tip: Similar to the previous two functions, you can use jQuery selectors in the RemoveItem function.

    There is another method for local storage that allows you to clear the entire local storage. We call the ClearAll() function in the onClick event of the “Clear” button:

    <input type="button" value="Clear" onclick="ClearAll()">
    

    We use the clear method to clear the local storage, as shown below:

    function ClearAll() {
        localStorage.clear();
        doShowAll();
    }
    

    Session Storage

    The sessionStorage object works in the same way as localStorage. You can replace the above example with the sessionStorage object to expire the data after one session. Once the user closes the browser window, the storage will be cleared. In short, the APIs for localStorage and sessionStorage are identical, allowing you to use the following methods:

    • setItem(key, value)
    • getItem(key)
    • removeItem(key)
    • clear()
    • key(index)
    • length

    Shopping Carts With Arrays And Objects

    Because HTML5 web storage only supports single name-value storage, you have to use JSON or another method to convert your arrays or objects into a single string. You might need an array or object if you have a category and subcategories of items, or if you have a shopping cart with multiple data, like customer info, item info, etc. You just need to implode your array or object items into a string to store them in web storage, and then explode (or split) them back to an array to show them on another page. Let’s go through a basic example of a shopping cart that has three sets of info: customer info, item info and custom mailing address. First, we use JSON.stringify to convert the object into a string. Then, we use JSON.parse to reverse it back.

    Hint: Keep in mind that the key-name should be unique for each domain.

    //Customer info
    //You can use array in addition to object.
    var obj1 = { firstname: "John", lastname: "thomson" };
    var cust = JSON.stringify(obj1);
    
    //Mailing info
    var obj2 = { state: "VA", city: "Arlington" };
    var mail = JSON.stringify(obj2);
    
    //Item info
    var obj3 = { item: "milk", quantity: 2 };
    var basket = JSON.stringify(obj3);
    
    //Next, push three strings into key-value of HTML5 storage.
    
    //Use JSON parse function below to convert string back into object or array.
    var New_cust=JSON.parse(cust);
    

    Summary

    In this tutorial, we have learned how to build a shopping cart step by step using HTML5 web storage and JavaScript. We’ve seen how to use jQuery as an alternative to JavaScript. We’ve also discussed JSON functions like stringify and parse to handle arrays and objects in a shopping cart. You can build on this tutorial by adding more features, like adding product categories and subcategories while storing data in a JavaScript multi-dimensional array. Moreover, you can replace the whole JavaScript code with jQuery.

    We’ve seen what other things developers can accomplish with HTML5 web storage and what other features they can add to their websites. For example, you can use this tutorial to store user preferences, favorited content, wish lists, and user settings like names and passwords on websites and native mobile apps, without using a database.

    To conclude, here are a few issues to consider when implementing HTML5 web storage:

    • Some users might have privacy settings that prevent the browser from storing data.
    • Some users might use their browser in incognito mode.
    • Be aware of a few security issues, like DNS spoofing attacks, cross-directory attacks, and sensitive data compromise.

    Related Reading

    • “Browser Storage Limits And Eviction Criteria,” MDN web docs, Mozilla
    • “Web Storage,” HTML Living Standard,
    • “This Week In HTML 5,” The WHATWG Blog

    (dm, il)

    From our sponsors: Creating A Shopping Cart With HTML5 Web Storage

    Posted on 27th August 2019Web Design
    FacebookshareTwittertweetGoogle+share

    Related posts

    Archived
    22nd March 2023
    Archived
    18th March 2023
    Archived
    20th January 2023
    Thumbnail for 25788
    Handling Continuous Integration And Delivery With GitHub Actions
    19th October 2020
    Thumbnail for 25778
    A Monthly Update With New Guides And Community Resources
    19th October 2020
    Thumbnail for 25781
    Supercharge Testing React Applications With Wallaby.js
    19th October 2020
    Latest News
    • Archived
      22nd March 2023
    • Archived
      18th March 2023
    • Archived
      20th January 2023
    • 20201019 ML Brief
      19th October 2020
    • Thumbnail for 25788
      Handling Continuous Integration And Delivery With GitHub Actions
      19th October 2020
    • Thumbnail for 25786
      The Future of CX with Larry Ellison
      19th October 2020
    News Categories
    • Digital Marketing
    • Web Design

    Our services

    Website Design
    Website Design

    A website is an important part of any business. Professional website development is an essential element of a successful online business.

    We provide website design services for every type of website imaginable. We supply brochure websites, E-commerce websites, bespoke website design, custom website development and a range of website applications. We love developing websites, come and talk to us about your project and we will tailor make a solution to match your requirements.

    You can contact us by phone, email or send us a request through our online form and we can give you a call back.

    More Information

    Digital Marketing
    Digital Marketing

    Our digital marketeers have years of experience in developing and excuting digital marketing strategies. We can help you promote your business online with the most effective methods to achieve the greatest return for your marketing budget. We offer a full service with includes the following:

    1. Social Media Marketing

    2. Email & Newsletter Advertising

    3. PPC - Pay Per Click

    4. A range of other methods are available

    More Information

    SEO
    SEO Services

    SEO is an essential part of owning an online property. The higher up the search engines that your website appears, the more visitors you will have and therefore the greater the potential for more business and increased profits.

    We offer a range of SEO services and packages. Our packages are very popular due to the expanse of on-page and off-page SEO services that they cover. Contact us to discuss your website and the SEO services that would best suit to increase your websites ranking.

    More Information

    E-commerce
    E-commerce Websites

    E-commerce is a rapidly growing area with sales online increasing year on year. A professional E-commerce store online is essential to increase sales and is a reflection of your business to potential customers. We provide professional E-commerce websites custom built to meet our clients requirements.

    Starting to sell online can be a daunting task and we are here to make that journey as smooth as possible. When you work with Cunningham Web Solutions on your E-commerce website, you will benefit from the experience of our team and every detail from the website design to stock management is carefully planned and designed with you in mind.

    More Information

    Social Media Services
    Social Media Services

    Social Media is becoming an increasingly effective method of marketing online. The opportunities that social media marketing can offer are endless and when managed correctly can bring great benefits to every business.

    Social Media Marketing is a low cost form of advertising that continues to bring a very good ROI for our clients. In conjuction with excellent website development and SEO, social media marketing should be an essential part of every digital marketing strategy.

    We offer Social Media Management packages and we also offer Social Media Training to individuals and to companies. Contact us to find out more.

    More Information

    Cunningham Web Solutions
    © Copyright 2025 | Cunningham Web Solutions
    • Home
    • Our Services
    • FAQ's
    • Account Services
    • Privacy Policy
    • Contact Us