<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Storage in HTML5 &#8211; Sibeesh Passion</title>
	<atom:link href="https://mail.sibeeshpassion.com/tag/storage-in-html5/feed/" rel="self" type="application/rss+xml" />
	<link>https://mail.sibeeshpassion.com</link>
	<description>My passion towards life</description>
	<lastBuildDate>Wed, 11 Jul 2018 16:28:31 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>/wp-content/uploads/2017/04/Sibeesh_Passion_Logo_Small.png</url>
	<title>Storage in HTML5 &#8211; Sibeesh Passion</title>
	<link>https://mail.sibeeshpassion.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Introduction to IndexedDB</title>
		<link>https://mail.sibeeshpassion.com/introduction-to-indexeddb/</link>
					<comments>https://mail.sibeeshpassion.com/introduction-to-indexeddb/#disqus_thread</comments>
		
		<dc:creator><![CDATA[SibeeshVenu]]></dc:creator>
		<pubDate>Wed, 10 Feb 2016 00:00:39 +0000</pubDate>
				<category><![CDATA[HTML5]]></category>
		<category><![CDATA[Storage In HTML5]]></category>
		<category><![CDATA[IndexedDB]]></category>
		<category><![CDATA[IndexedDB And Web SQL]]></category>
		<category><![CDATA[openCursor()]]></category>
		<category><![CDATA[Storage in HTML5]]></category>
		<guid isPermaLink="false">http://sibeecst_passion.com/?p=11167</guid>

					<description><![CDATA[[toc] Introduction In this post, we will see some information about IndexedDB. As the name implies, IndexedDB is a database in which we can do all kind of operations we do in a normal database. This IndexedDB has been introduced with the HTML5 . This allows a user to store a large amount of data in user&#8217;s browser. It has been proved that IndexedDB is more powerful and efficient than other storage mechanisms like local storage and session storage. Basically, IndexedDB is an API which helps the developers to do some database operations in client side, like creating a database, [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>[toc]</p>
<h3>Introduction</h3>
<p>
In this post, we will see some information about IndexedDB. As the name implies, IndexedDB is a <a href="http://sibeeshpassion.com/category/database/" target="_blank">database </a>in which we can do all kind of operations we do in a normal database. This IndexedDB has been introduced with the <a href="http://sibeeshpassion.com/category/html5/" target="_blank">HTML5 </a>. This allows a user to store a large amount of data in user&#8217;s browser. It has been proved that IndexedDB is more powerful and efficient than other storage mechanisms like local storage and session storage. Basically, IndexedDB is an API which helps the developers to do some database operations in client side, like creating a database, open the transaction, creating tables, inserting values to tables, deleting values, reading the data. If you need any other way to save some data in client side, you can use <a href="http://sibeeshpassion.com/tag/storage-in-html5/" target="_blank">storage mechanisms </a> introduced in HTML5. Now we will look some of the operations a developer can do with IndexedDB. I hope you will like this.
</p>
<h3>Background</h3>
<p>
Before introducing IndexedDB, developers were familiar with Web SQL. If you need to know more about Web SQL you can see here: <a href="http://sibeeshpassion.com/introduction-to-web-sql/" target="_blank">Web SQL Introduction</a>.
</p>
<h3>Why to use IndexedDB instead Web SQL?</h3>
<p>
W3C has been announced that use of Web SQL is obsolete and deprecated, hence it is not recommended using Web SQL in your applications. Most of the modern web browsers like Mozilla does not support the use of Web SQL, this is also a great limitation of Web SQL. </p>
<p>Now we have an alternative to Web SQL, IndexedDB which more efficient and faster than Web SQL. Below are some of the main advantages of IndexedDB.</p>
<li>It stores the data as Key-Pair values</li>
<li>It is Asynchronous</li>
<li>It is non relational</li>
<li>Can access the data from the same domain</li>
<p><h3>Using the code</h3>
<p>
As you all know, the first step to work with a database is just creating it. </p>
<h4>Create/Open IndexedDB Database</h4>
<p>It is always better to check whether your browser is supporting IndexedDB or not. To check that you can use the following syntax. </p>
<p>[js]<br />
&lt;script type=&quot;text/javascript&quot;&gt;<br />
        window.indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB;<br />
        //check whether the database is created or not.<br />
        if (!window.indexedDB) {<br />
            alert(&quot;Oops, Does not support IndexedDB&quot;);<br />
        }<br />
    &lt;/script&gt;<br />
[/js]</p>
<p>So if your browser is supporting IndexedDB, you are ready to go!. Cool!. Now we can create a database.  </p>
<p>[js]<br />
//check whether the database is created or not.<br />
        if (!window.indexedDB) {<br />
            alert(&quot;Oops, Does not support IndexedDB&quot;);<br />
        } else {<br />
            //Create database<br />
            var dbName = &#8216;myDB&#8217;;// Database name<br />
            var dbVersion = 2;// Database version<br />
            var crDB = window.indexedDB.open(dbName, dbVersion);<br />
        }<br />
[/js]</p>
<p>Here the first parameter is the database name and the second one is the version of your database. A version represents your database&#8217;s current schema. If everything goes well, you can see your database in your browser&#8217;s resources as follows.</p>
<div id="attachment_11168" style="width: 614px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2016/02/IndexedDB-Creation.png"><img fetchpriority="high" decoding="async" aria-describedby="caption-attachment-11168" src="http://sibeeshpassion.com/wp-content/uploads/2016/02/IndexedDB-Creation.png" alt="IndexedDB Creation" width="604" height="255" class="size-full wp-image-11168" srcset="/wp-content/uploads/2016/02/IndexedDB-Creation.png 604w, /wp-content/uploads/2016/02/IndexedDB-Creation-300x127.png 300w, /wp-content/uploads/2016/02/IndexedDB-Creation-400x169.png 400w" sizes="(max-width: 604px) 100vw, 604px" /></a><p id="caption-attachment-11168" class="wp-caption-text">IndexedDB Creation</p></div>
<p>When you create a database there some certain events are getting fired, they are</p>
<h5>onupgradeneeded</h5>
<p>[js]<br />
crDB.onupgradeneeded = function (event) {<br />
                alert(&quot;That&#8217;s cool, we are upgrading&quot;);<br />
                db = event.target.result;<br />
                var objectStore = db.createObjectStore(&quot;UserName&quot;, { keyPath: &quot;UserID&quot; });<br />
            };<br />
[/js]</p>
<p>This event gets fired when you try upgrading your database. Here we are creating an object store with name &#8216;UserName&#8217; and index key &#8216;UserID&#8217;. Below is the output you get when the onupgradeneeded is fired. </p>
<div id="attachment_11171" style="width: 455px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2016/02/IndexedDB-onupgradeneeded-Alert.png"><img decoding="async" aria-describedby="caption-attachment-11171" src="http://sibeeshpassion.com/wp-content/uploads/2016/02/IndexedDB-onupgradeneeded-Alert.png" alt="IndexedDB onupgradeneeded Alert" width="445" height="179" class="size-full wp-image-11171" srcset="/wp-content/uploads/2016/02/IndexedDB-onupgradeneeded-Alert.png 445w, /wp-content/uploads/2016/02/IndexedDB-onupgradeneeded-Alert-300x121.png 300w, /wp-content/uploads/2016/02/IndexedDB-onupgradeneeded-Alert-400x161.png 400w" sizes="(max-width: 445px) 100vw, 445px" /></a><p id="caption-attachment-11171" class="wp-caption-text">IndexedDB onupgradeneeded Alert</p></div>
<div id="attachment_11172" style="width: 660px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2016/02/IndexedDB-onupgradeneeded-Output-e1454589883218.png"><img decoding="async" aria-describedby="caption-attachment-11172" src="http://sibeeshpassion.com/wp-content/uploads/2016/02/IndexedDB-onupgradeneeded-Output-e1454589883218.png" alt="IndexedDB onupgradeneeded Output" width="650" height="202" class="size-full wp-image-11172" srcset="/wp-content/uploads/2016/02/IndexedDB-onupgradeneeded-Output-e1454589883218.png 650w, /wp-content/uploads/2016/02/IndexedDB-onupgradeneeded-Output-e1454589883218-300x93.png 300w, /wp-content/uploads/2016/02/IndexedDB-onupgradeneeded-Output-e1454589883218-400x124.png 400w" sizes="(max-width: 650px) 100vw, 650px" /></a><p id="caption-attachment-11172" class="wp-caption-text">IndexedDB onupgradeneeded Output</p></div>
<h5>onsuccess</h5>
<p>[js]<br />
crDB.onsuccess = function (event) {<br />
                alert(&quot;Awesome, You have successfully Opened a Databse!&quot;);<br />
                db = event.target.result;<br />
            }<br />
[/js]</p>
<p>If there are no errors, you will get an alert as follows.</p>
<div id="attachment_11170" style="width: 458px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2016/02/IndexedDB-OnSuccess-Alert.png"><img decoding="async" aria-describedby="caption-attachment-11170" src="http://sibeeshpassion.com/wp-content/uploads/2016/02/IndexedDB-OnSuccess-Alert.png" alt="IndexedDB OnSuccess Alert" width="448" height="181" class="size-full wp-image-11170" srcset="/wp-content/uploads/2016/02/IndexedDB-OnSuccess-Alert.png 448w, /wp-content/uploads/2016/02/IndexedDB-OnSuccess-Alert-300x121.png 300w, /wp-content/uploads/2016/02/IndexedDB-OnSuccess-Alert-400x162.png 400w" sizes="(max-width: 448px) 100vw, 448px" /></a><p id="caption-attachment-11170" class="wp-caption-text">IndexedDB OnSuccess Alert</p></div>
<h5>onerror</h5>
<p>[js]<br />
 crDB.onerror = function (event) {<br />
                alert(&quot;Oops, There is error!&quot;, event);<br />
            }<br />
[/js]</p>
<p>Here crDB is our database instance. Once this is done, we can start using the transaction as we use in SQL. </p>
<h4>Creating transaction</h4>
<p>To create a transaction we can use the following syntax. We can use <em>transaction</em> method from our database instance.</p>
<p>[js]<br />
//Creating transaction<br />
            var myTran = db.transaction([&quot;UserName&quot;], &quot;readwrite&quot;);</p>
<p>            myTran.oncomplete = function (e) {<br />
                alert(&quot;Awesome, Transaction is successfully created!&quot;);<br />
            };</p>
<p>            myTran.onerror = function (e) {<br />
                alert(&quot;Oops, There is error!&quot;, e.message);<br />
            };<br />
[/js]</p>
<p>Here db is our database instance. And myTran is our transaction object which we are going to use for our upcoming operations. We are creating a transaction with read-write permission, that is why we have given a property as &#8216;readwrite&#8217; while creating a transaction. Why we use transaction is, as you all know transaction can be rollbacked. For example, if any of the operation throws an error, the transaction will be rollbacked so there won&#8217;t be any kind of mismatching data happening. And of course, we can easily manage error logs with the help of transaction. Shall we write queries needed for our operations?</p>
<h4>Adding the data</h4>
<p>Once our transaction is ready we can insert some data into our object in the transaction on success event, which we have called inside of database onsuccess event. Please check the below code.</p>
<p>[js]<br />
crDB.onsuccess = function (event) {<br />
                alert(&quot;Awesome, You have successfully Opened a Databse!&quot;);<br />
                db = event.target.result;</p>
<p>                //Creating transaction<br />
                var myTran = db.transaction([&quot;UserName&quot;], &quot;readwrite&quot;);</p>
<p>                myTran.oncomplete = function (e) {<br />
                    alert(&quot;Awesome, Transaction is successfully created!&quot;);<br />
                };</p>
<p>                myTran.onsuccess = function (e) {<br />
                    alert(&quot;Awesome, Transaction is successfully created!&quot;);<br />
                    //Adding the data<br />
                    var myObj = myTran.objectStore(&quot;UserName&quot;);</p>
<p>                    var myNames = [&quot;Sibi&quot;, &quot;Aji&quot;, &quot;Ansu&quot;, &quot;Shantu&quot;, &quot;Aghil&quot;];//Array with names</p>
<p>                    for (var i = 0; i &lt; 5; i++) {//Adding 5 objects<br />
                        myObj.add({ UserID: &#8216;SBKL0&#8217; + i, Name: myNames[i] });<br />
                    }<br />
                };</p>
<p>                myTran.onerror = function (e) {<br />
                    alert(&quot;Oops, There is error!&quot;, e.message);<br />
                };</p>
<p>            }<br />
[/js]</p>
<p>Here myNames is the array in which we have few names and we are using a loop to insert few data to the object. Now you can see that your data have been added to the object in your browser&#8217;s resources. </p>
<div id="attachment_11176" style="width: 660px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2016/02/IndexedDB-Adding-Data-To-Objects-e1454657780204.png"><img decoding="async" aria-describedby="caption-attachment-11176" src="http://sibeeshpassion.com/wp-content/uploads/2016/02/IndexedDB-Adding-Data-To-Objects-e1454657780204.png" alt="IndexedDB Adding Data To Objects" width="650" height="159" class="size-full wp-image-11176" srcset="/wp-content/uploads/2016/02/IndexedDB-Adding-Data-To-Objects-e1454657780204.png 650w, /wp-content/uploads/2016/02/IndexedDB-Adding-Data-To-Objects-e1454657780204-300x73.png 300w, /wp-content/uploads/2016/02/IndexedDB-Adding-Data-To-Objects-e1454657780204-400x98.png 400w" sizes="(max-width: 650px) 100vw, 650px" /></a><p id="caption-attachment-11176" class="wp-caption-text">IndexedDB Adding Data To Objects</p></div>
<p>We have successfully added data. Right? Now we will see how we can remove the data we just added 🙂</p>
<h4>Removing the data</h4>
<p>If there is adding, there should be deleting :). You can delete an object from object store as follows.</p>
<p>[js]<br />
setTimeout(function () {<br />
                        db.transaction([&quot;UserName&quot;], &quot;readwrite&quot;).objectStore(&quot;UserName&quot;).delete(&#8216;SBKL00&#8217;);<br />
                    }, 10000);<br />
[/js]</p>
<p>So you can rewrite the on success event of transaction s follows. </p>
<p>[js]<br />
myTran.onsuccess = function (e) {<br />
                    alert(&quot;Awesome, Transaction is successfully created!&quot;);<br />
                    //Adding the data<br />
                    var myObj = myTran.objectStore(&quot;UserName&quot;);</p>
<p>                    var myNames = [&quot;Sibi&quot;, &quot;Aji&quot;, &quot;Ansu&quot;, &quot;Shantu&quot;, &quot;Aghil&quot;];//Array with names</p>
<p>                    for (var i = 0; i &lt; 5; i++) {//Adding 5 objects<br />
                        myObj.add({ UserID: &#8216;SBKL0&#8217; + i, Name: myNames[i] });<br />
                    }</p>
<p>                    setTimeout(function () {<br />
                        db.transaction([&quot;UserName&quot;], &quot;readwrite&quot;).objectStore(&quot;UserName&quot;).delete(&#8216;SBKL00&#8217;);<br />
                    }, 10000);<br />
                };<br />
[/js]</p>
<p>It can give you an output as follows.</p>
<div id="attachment_11177" style="width: 644px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2016/02/IndexedDB-Deleting-Data-From-Objects.png"><img decoding="async" aria-describedby="caption-attachment-11177" src="http://sibeeshpassion.com/wp-content/uploads/2016/02/IndexedDB-Deleting-Data-From-Objects-1024x242.png" alt="IndexedDB Deleting Data From Objects" width="634" height="150" class="size-large wp-image-11177" srcset="/wp-content/uploads/2016/02/IndexedDB-Deleting-Data-From-Objects-1024x242.png 1024w, /wp-content/uploads/2016/02/IndexedDB-Deleting-Data-From-Objects-300x71.png 300w, /wp-content/uploads/2016/02/IndexedDB-Deleting-Data-From-Objects-768x182.png 768w, /wp-content/uploads/2016/02/IndexedDB-Deleting-Data-From-Objects-400x95.png 400w, /wp-content/uploads/2016/02/IndexedDB-Deleting-Data-From-Objects.png 1036w" sizes="(max-width: 634px) 100vw, 634px" /></a><p id="caption-attachment-11177" class="wp-caption-text">IndexedDB Deleting Data From Objects</p></div>
<p>Now we will see how we can access a particular data from the object store. </p>
<h4>Access the data with key</h4>
<p>To access a particular data, we will add a button and in the button click event, we will fetch the details needed as a request. </p>
<p>[html]<br />
 &lt;input id=&quot;btnGet&quot; type=&quot;button&quot; value=&quot;Get Name&quot; /&gt;<br />
[/html]</p>
<p>Click event is as follows.</p>
<p>[js]<br />
$(function () {<br />
            $(&#8216;#btnGet&#8217;).click(function () {<br />
                var get = db.transaction([&quot;UserName&quot;], &quot;readwrite&quot;).objectStore(&quot;UserName&quot;).get(&#8216;SBKL01&#8217;);<br />
                get.onsuccess = function (event) {<br />
                    alert(&quot;The name you have requested is : &quot; + get.result.Name);<br />
                };</p>
<p>            });<br />
        });<br />
[/js]</p>
<p>You can get the alert as follows, if your run your code. </p>
<div id="attachment_11179" style="width: 528px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2016/02/IndexedDB-Get-The-Data-With-Key.png"><img decoding="async" aria-describedby="caption-attachment-11179" src="http://sibeeshpassion.com/wp-content/uploads/2016/02/IndexedDB-Get-The-Data-With-Key.png" alt="IndexedDB Get The Data With Key" width="518" height="340" class="size-full wp-image-11179" srcset="/wp-content/uploads/2016/02/IndexedDB-Get-The-Data-With-Key.png 518w, /wp-content/uploads/2016/02/IndexedDB-Get-The-Data-With-Key-300x197.png 300w, /wp-content/uploads/2016/02/IndexedDB-Get-The-Data-With-Key-400x263.png 400w" sizes="(max-width: 518px) 100vw, 518px" /></a><p id="caption-attachment-11179" class="wp-caption-text">IndexedDB Get The Data With Key</p></div>
<p>Now we will see how we can update data with the Get method. </p>
<h4>Updating the data</h4>
<p>To update the data we will have a button and in the button click we will fire the needed code. </p>
<p>[html]<br />
 &lt;input id=&quot;btnUpdate&quot; type=&quot;button&quot; value=&quot;Update Name&quot; /&gt;<br />
[/html]</p>
<p>And click event is as follows.</p>
<p>[js]<br />
$(&#8216;#btnUpdate&#8217;).click(function () {<br />
                var updObject = db.transaction([&quot;UserName&quot;], &quot;readwrite&quot;).objectStore(&quot;UserName&quot;);<br />
                var upd=updObject.get(&#8216;SBKL03&#8217;);<br />
                upd.onsuccess = function (event) {<br />
                    upd.result.Name = &#8216;New Name&#8217;;<br />
                    updObject.put(upd.result);<br />
                };</p>
<p>            });<br />
[/js]</p>
<p>Here we are getting an element using the get method, and we are updating the value in the result, once it is done, we will assign that new result to the object store using put method.</p>
<p>It will give you an output as follows.</p>
<div id="attachment_11180" style="width: 644px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2016/02/IndexedDB-Update-The-Data-With-Key.png"><img decoding="async" aria-describedby="caption-attachment-11180" src="http://sibeeshpassion.com/wp-content/uploads/2016/02/IndexedDB-Update-The-Data-With-Key-1024x247.png" alt="IndexedDB Update The Data With Key" width="634" height="153" class="size-large wp-image-11180" srcset="/wp-content/uploads/2016/02/IndexedDB-Update-The-Data-With-Key-1024x247.png 1024w, /wp-content/uploads/2016/02/IndexedDB-Update-The-Data-With-Key-300x72.png 300w, /wp-content/uploads/2016/02/IndexedDB-Update-The-Data-With-Key-768x185.png 768w, /wp-content/uploads/2016/02/IndexedDB-Update-The-Data-With-Key-400x96.png 400w, /wp-content/uploads/2016/02/IndexedDB-Update-The-Data-With-Key.png 1057w" sizes="(max-width: 634px) 100vw, 634px" /></a><p id="caption-attachment-11180" class="wp-caption-text">IndexedDB Update The Data With Key</p></div>
<p>Another interesting thing is, we can use cursors also as we use in SQL if we need to loop through some data. Here I will show you how we can do that. </p>
<h4>Using Cursor</h4>
<p>Create a button and write few code as  follows.</p>
<p>[html]<br />
&lt;input id=&quot;btnShow&quot; type=&quot;button&quot; value=&quot;Show the data&quot; /&gt;<br />
[/html]</p>
<p>Then we will create an HTML table.</p>
<p>[html]<br />
&lt;div id=&quot;myTab&quot;&gt;<br />
        &lt;table id=&quot;show&quot;&gt;<br />
            &lt;thead&gt;<br />
                &lt;th&gt;User ID &lt;/th&gt;<br />
                &lt;th&gt;Name&lt;/th&gt;<br />
            &lt;/thead&gt;<br />
            &lt;tbody&gt;&lt;/tbody&gt;<br />
        &lt;/table&gt;<br />
    &lt;/div&gt;<br />
[/html]</p>
<p>Now we can write the cursor as follows. </p>
<p>[js]<br />
$(&#8216;#btnShow&#8217;).click(function () {<br />
                var curObject = db.transaction([&quot;UserName&quot;], &quot;readwrite&quot;).objectStore(&quot;UserName&quot;);<br />
                curObject.openCursor().onsuccess = function (event) { //Opening the cursor<br />
                    var cur = event.target.result;<br />
                    if (cur) {// Checks for the cursor<br />
                        $(&#8216;#show&#8217;).append(&#8216;&lt;tr&gt;&lt;td&gt;&#8217; + cur.key + &#8216;&lt;/td&gt;&lt;td&gt;&#8217; + cur.value.Name + &#8216;&lt;/td&gt;&lt;/tr&gt;&#8217;);<br />
                        cur.continue();<br />
                    }<br />
                };<br />
                $(&#8216;#myTab&#8217;).show();<br />
            });<br />
[/js]</p>
<p>Here we are creating a cursor using the function <em>openCursor()</em> and in the onsuccess event we are looping through the cursor. You can always give some styles to your table.</p>
<p>[css]<br />
&lt;style&gt;<br />
        table, tr, td, th {<br />
            border: 1px solid #ccc;<br />
            border-radius: 5px;<br />
            padding: 10px;<br />
            margin: 10px;<br />
        }<br />
    &lt;/style&gt;<br />
[/css]</p>
<p>That is all 🙂 Now when you click the show button, you can see the data in a table format as follows. </p>
<div id="attachment_11183" style="width: 311px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2016/02/IndexedDB-Using-Cursor.png"><img decoding="async" aria-describedby="caption-attachment-11183" src="http://sibeeshpassion.com/wp-content/uploads/2016/02/IndexedDB-Using-Cursor.png" alt="IndexedDB Using Cursor" width="301" height="296" class="size-full wp-image-11183" srcset="/wp-content/uploads/2016/02/IndexedDB-Using-Cursor.png 301w, /wp-content/uploads/2016/02/IndexedDB-Using-Cursor-300x295.png 300w" sizes="(max-width: 301px) 100vw, 301px" /></a><p id="caption-attachment-11183" class="wp-caption-text">IndexedDB Using Cursor</p></div>
<h3>Complete code</h3>
<p>
[html]<br />
&lt;!DOCTYPE html&gt;<br />
&lt;html&gt;<br />
&lt;head&gt;<br />
    &lt;title&gt;Introduction to IndexedDB&lt;/title&gt;<br />
    &lt;script src=&quot;Scripts/jquery-1.11.1.min.js&quot;&gt;&lt;/script&gt;<br />
    &lt;script type=&quot;text/javascript&quot;&gt;<br />
        var dbName = &#8216;myDB&#8217;;// Database name<br />
        var dbVersion = 2;// Database version<br />
        var crDB;<br />
        var db;<br />
        window.indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB;<br />
        //check whether the database is created or not.<br />
        if (!window.indexedDB) {<br />
            alert(&quot;Oops, Does not support IndexedDB&quot;);<br />
        } else {<br />
            //Create database<br />
            crDB = window.indexedDB.open(dbName, dbVersion);<br />
            var db;<br />
            crDB.onerror = function (event) {<br />
                alert(&quot;Oops, There is error!&quot;, event);<br />
            }<br />
            crDB.onupgradeneeded = function (event) {<br />
                alert(&quot;That&#8217;s cool, we are upgrading&quot;);<br />
                db = event.target.result;<br />
                var objectStore = db.createObjectStore(&quot;UserName&quot;, { keyPath: &quot;UserID&quot; });<br />
            };<br />
            crDB.onsuccess = function (event) {<br />
                alert(&quot;Awesome, You have successfully Opened a Databse!&quot;);<br />
                db = event.target.result;</p>
<p>                //Creating transaction<br />
                var myTran = db.transaction([&quot;UserName&quot;], &quot;readwrite&quot;);</p>
<p>                myTran.oncomplete = function (e) {<br />
                    alert(&quot;Awesome, Transaction is successfully created!&quot;);<br />
                };</p>
<p>                myTran.onsuccess = function (e) {<br />
                    alert(&quot;Awesome, Transaction is successfully created!&quot;);<br />
                    //Adding the data<br />
                    var myObj = myTran.objectStore(&quot;UserName&quot;);</p>
<p>                    var myNames = [&quot;Sibi&quot;, &quot;Aji&quot;, &quot;Ansu&quot;, &quot;Shantu&quot;, &quot;Aghil&quot;];//Array with names</p>
<p>                    for (var i = 0; i &lt; 5; i++) {//Adding 5 objects<br />
                        myObj.add({ UserID: &#8216;SBKL0&#8217; + i, Name: myNames[i] });<br />
                    }</p>
<p>                    setTimeout(function () {<br />
                        db.transaction([&quot;UserName&quot;], &quot;readwrite&quot;).objectStore(&quot;UserName&quot;).delete(&#8216;SBKL00&#8217;);<br />
                    }, 10000);<br />
                };</p>
<p>                myTran.onerror = function (e) {<br />
                    alert(&quot;Oops, There is error!&quot;, e.message);<br />
                };</p>
<p>            }<br />
        }<br />
        $(function () {<br />
            $(&#8216;#myTab&#8217;).hide();<br />
            $(&#8216;#btnGet&#8217;).click(function () {<br />
                var get = db.transaction([&quot;UserName&quot;], &quot;readwrite&quot;).objectStore(&quot;UserName&quot;).get(&#8216;SBKL01&#8217;);<br />
                get.onsuccess = function (event) {<br />
                    alert(&quot;The name you have requested is : &quot; + get.result.Name);<br />
                };</p>
<p>            });<br />
            $(&#8216;#btnUpdate&#8217;).click(function () {<br />
                var updObject = db.transaction([&quot;UserName&quot;], &quot;readwrite&quot;).objectStore(&quot;UserName&quot;);<br />
                var upd = updObject.get(&#8216;SBKL03&#8217;);<br />
                upd.onsuccess = function (event) {<br />
                    upd.result.Name = &#8216;New Name&#8217;;<br />
                    updObject.put(upd.result);<br />
                };</p>
<p>            });<br />
            $(&#8216;#btnShow&#8217;).click(function () {<br />
                var curObject = db.transaction([&quot;UserName&quot;], &quot;readwrite&quot;).objectStore(&quot;UserName&quot;);<br />
                curObject.openCursor().onsuccess = function (event) { //Opening the cursor<br />
                    var cur = event.target.result;<br />
                    if (cur) {// Checks for the cursor<br />
                        $(&#8216;#show&#8217;).append(&#8216;&lt;tr&gt;&lt;td&gt;&#8217; + cur.key + &#8216;&lt;/td&gt;&lt;td&gt;&#8217; + cur.value.Name + &#8216;&lt;/td&gt;&lt;/tr&gt;&#8217;);<br />
                        cur.continue();<br />
                    }<br />
                };<br />
                $(&#8216;#myTab&#8217;).show();<br />
            });<br />
        });<br />
    &lt;/script&gt;<br />
    &lt;style&gt;<br />
        table, tr, td, th {<br />
            border: 1px solid #ccc;<br />
            border-radius: 5px;<br />
            padding: 10px;<br />
            margin: 10px;<br />
        }<br />
    &lt;/style&gt;<br />
&lt;/head&gt;<br />
&lt;body&gt;<br />
    &lt;input id=&quot;btnGet&quot; type=&quot;button&quot; value=&quot;Get Name&quot; /&gt;<br />
    &lt;input id=&quot;btnUpdate&quot; type=&quot;button&quot; value=&quot;Update Name&quot; /&gt;<br />
    &lt;input id=&quot;btnShow&quot; type=&quot;button&quot; value=&quot;Show the data&quot; /&gt;<br />
    &lt;div id=&quot;myTab&quot;&gt;<br />
        &lt;table id=&quot;show&quot;&gt;<br />
            &lt;thead&gt;<br />
                &lt;th&gt;User ID &lt;/th&gt;<br />
                &lt;th&gt;Name&lt;/th&gt;<br />
            &lt;/thead&gt;<br />
            &lt;tbody&gt;&lt;/tbody&gt;<br />
        &lt;/table&gt;<br />
    &lt;/div&gt;<br />
&lt;/body&gt;<br />
&lt;/html&gt;<br />
[/html]</p>
<p>That is all. We did it. Have a happy coding.
</p>
<h3>Conclusion</h3>
<p>
Did I miss anything that you may think which is needed? Did you try Web SQL yet? Have you ever wanted to do this requirement? Could you find this post as useful? I hope you liked this article. Please share me your valuable suggestions and feedback.
</p>
<h3>Your turn. What do you think?</h3>
<p>
A blog isn’t a blog without comments, but do try to stay on topic. If you have a question unrelated to this post, you’re better off posting it on C# Corner, Code Project, Stack Overflow, Asp.Net Forum instead of commenting here. Tweet or email me a link to your question there and I’ll definitely try to help if I can.
</p>
<p>Kindest Regards<br />
Sibeesh Venu</p>
]]></content:encoded>
					
					<wfw:commentRss>https://mail.sibeeshpassion.com/introduction-to-indexeddb/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Remove Filtered sessionStorage And localStorage</title>
		<link>https://mail.sibeeshpassion.com/remove-filtered-sessionstorage-and-localstorage/</link>
					<comments>https://mail.sibeeshpassion.com/remove-filtered-sessionstorage-and-localstorage/#disqus_thread</comments>
		
		<dc:creator><![CDATA[SibeeshVenu]]></dc:creator>
		<pubDate>Tue, 11 Aug 2015 00:32:47 +0000</pubDate>
				<category><![CDATA[HTML5]]></category>
		<category><![CDATA[Storage In HTML5]]></category>
		<category><![CDATA[Find Out Particular Local Storage And Delete]]></category>
		<category><![CDATA[Find Out Particular Session Storage And Delete]]></category>
		<category><![CDATA[Local Storage]]></category>
		<category><![CDATA[Remove Filtered sessionStorage And localStorage]]></category>
		<category><![CDATA[Remove Local Storage]]></category>
		<category><![CDATA[Remove session storage]]></category>
		<category><![CDATA[Session Storage]]></category>
		<category><![CDATA[Storage in HTML5]]></category>
		<guid isPermaLink="false">http://sibeecst_passion.com/?p=8541</guid>

					<description><![CDATA[[toc] Introduction In this post we well discuss how to remove all local storage and session storage or remove the storage according to the key values, for example, is a key contain a particular string, we will remove that local storage and session storage. We will find all the local storage and session storage first and then we will loop through each, if a user needs to delete a particular storage according to a particular condition, we will delete that storage alone. Both localStorage and sessionStorage has been introduced with HTML5. If you are new to storage mechanism in client [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>[toc]</p>
<h3>Introduction</h3>
<p>
In this post we well discuss how to remove all local storage and session storage or remove the storage according to the key values, for example, is a key contain a particular string, we will remove that local storage and session storage. We will find all the local storage and session storage first and then we will loop through each, if a user needs to delete a particular storage according to a particular condition, we will delete that storage alone. </p>
<p>Both <em>localStorage</em> and <em>sessionStorage </em>has been introduced with HTML5. If you are new to storage mechanism in client side, you can find out here: <a href="http://sibeeshpassion.com/tag/storage-in-html5/" target="_blank">Storage In HTML5</a>
</p>
<h3>Backgroud</h3>
<p>
I am working in a client-side application where we use <em>localStorage</em> and <em>sessionStorage </em> for saving some key informations. What I mean by key information is, like we store the mail id of the user who uses the application. There I got a requirement of deleting the <em>localStorage</em> and <em>sessionStorage </em> according to the key value. For example, I needed to delete the storage values whose keys start with &#8220;logged&#8221;. I did the requirement and thought to share with you all. I hope someone will find it is useful.
</p>
<h3>Using the code</h3>
<p>
To start with you need to include jquery reference.</p>
<p>[js]<br />
 &lt;script src=&quot;https://code.jquery.com/jquery-2.1.4.min.js&quot;&gt;&lt;/script&gt;<br />
[/js]</p>
<p>Now we need to set the <em>localStorage</em> and <em>sessionStorage </em> right? </p>
<p>Add the storage values in the ready event.</p>
<p>[js]<br />
localStorage.setItem(&quot;First1&quot;,&quot;First Local Storage&quot;);<br />
			sessionStorage.setItem(&quot;First1&quot;,&quot;First Session Storage&quot;);<br />
			localStorage.setItem(&quot;Second1&quot;,&quot;Second Local Storage&quot;);<br />
			sessionStorage.setItem(&quot;Second1&quot;,&quot;Second Session Storage&quot;);<br />
[/js]</p>
<p>So we have set our <em>localStorage</em> and <em>sessionStorage </em>. Now we need to check whether it is saved or not right? Just run your page and see your browser console.</p>
<p><a href="http://sibeeshpassion.com/wp-content/uploads/2015/08/Remove_Filtered_sessionStorage_And_localStorage1.png"><img decoding="async" src="http://sibeeshpassion.com/wp-content/uploads/2015/08/Remove_Filtered_sessionStorage_And_localStorage1-1024x256.png" alt="Remove_Filtered_sessionStorage_And_localStorage1" width="634" height="159" class="alignnone size-large wp-image-8551" srcset="/wp-content/uploads/2015/08/Remove_Filtered_sessionStorage_And_localStorage1-1024x256.png 1024w, /wp-content/uploads/2015/08/Remove_Filtered_sessionStorage_And_localStorage1-300x75.png 300w, /wp-content/uploads/2015/08/Remove_Filtered_sessionStorage_And_localStorage1-768x192.png 768w, /wp-content/uploads/2015/08/Remove_Filtered_sessionStorage_And_localStorage1-400x100.png 400w, /wp-content/uploads/2015/08/Remove_Filtered_sessionStorage_And_localStorage1.png 634w" sizes="(max-width: 634px) 100vw, 634px" /></a></p>
<p><a href="http://sibeeshpassion.com/wp-content/uploads/2015/08/Remove_Filtered_sessionStorage_And_localStorage2-e1438859495855.png"><img decoding="async" src="http://sibeeshpassion.com/wp-content/uploads/2015/08/Remove_Filtered_sessionStorage_And_localStorage2-e1438859495855.png" alt="Remove_Filtered_sessionStorage_And_localStorage2" width="650" height="177" class="alignnone size-full wp-image-8561" srcset="/wp-content/uploads/2015/08/Remove_Filtered_sessionStorage_And_localStorage2-e1438859495855.png 650w, /wp-content/uploads/2015/08/Remove_Filtered_sessionStorage_And_localStorage2-e1438859495855-300x82.png 300w, /wp-content/uploads/2015/08/Remove_Filtered_sessionStorage_And_localStorage2-e1438859495855-400x109.png 400w" sizes="(max-width: 650px) 100vw, 650px" /></a></p>
<p>Now we will add some elements where we can fire the delete events. Cool?</p>
<p>[html]<br />
 &lt;a href=&quot;#&quot; id=&quot;removeAllLocalStorage&quot;&gt;Click To Remove All Local Storage&lt;/a&gt;&lt;br/&gt;&lt;br/&gt;<br />
   &lt;a href=&quot;#&quot; id=&quot;removeAllLocalStorageWhichStarts&quot;&gt;Click To Remove All Local Storage Which Starts With &quot;First&quot;&lt;/a&gt;&lt;br/&gt;&lt;br/&gt;<br />
   &lt;a href=&quot;#&quot; id=&quot;removeAllSessionStorage&quot;&gt;Click To Remove All Session Storage&lt;/a&gt;&lt;br/&gt;&lt;br/&gt;<br />
   &lt;a href=&quot;#&quot; id=&quot;removeAllSessionStorageWhichStarts&quot;&gt;Click To Remove All Session Storage Which Starts With &quot;First&quot;&lt;/a&gt;&lt;br/&gt;&lt;br/&gt;<br />
[/html]</p>
<p>Once that is done, we add the click event scripts.</p>
<p>[js]<br />
 &lt;script&gt;<br />
       $(document).ready(function(){<br />
			localStorage.setItem(&quot;First1&quot;,&quot;First Local Storage&quot;);<br />
			sessionStorage.setItem(&quot;First1&quot;,&quot;First Session Storage&quot;);<br />
			localStorage.setItem(&quot;Second1&quot;,&quot;Second Local Storage&quot;);<br />
			sessionStorage.setItem(&quot;Second1&quot;,&quot;Second Session Storage&quot;);</p>
<p>            $(&quot;#removeAllLocalStorage&quot;).click(function(){<br />
				Object.keys(localStorage)<br />
				.forEach(function (key) {<br />
						localStorage.removeItem(key);<br />
				});<br />
            });<br />
			$(&quot;#removeAllLocalStorageWhichStarts&quot;).click(function(){<br />
				Object.keys(localStorage)<br />
				.forEach(function (key) {<br />
						if ((/^First/.test(key))) {<br />
							localStorage.removeItem(key);<br />
						}<br />
				});<br />
            });<br />
			$(&quot;#removeAllSessionStorage&quot;).click(function(){<br />
				Object.keys(sessionStorage)<br />
				.forEach(function (key) {<br />
						sessionStorage.removeItem(key);<br />
				});<br />
            });<br />
			$(&quot;#removeAllSessionStorageWhichStarts&quot;).click(function(){<br />
				Object.keys(sessionStorage)<br />
				.forEach(function (key) {<br />
						if ((/^First/.test(key))) {<br />
							sessionStorage.removeItem(key);<br />
						}<br />
				});<br />
            });<br />
       });<br />
      &lt;/script&gt;<br />
[/js]	 </p>
<p>So everything is set. Now the only pending thing is just run and see the output. </p>
<p>First, we will fire the click event which removes all the local storage. ok? </p>
<p><a href="http://sibeeshpassion.com/wp-content/uploads/2015/08/Remove_Filtered_sessionStorage_And_localStorage3-e1438859851133.png"><img decoding="async" src="http://sibeeshpassion.com/wp-content/uploads/2015/08/Remove_Filtered_sessionStorage_And_localStorage3-e1438859851133.png" alt="Remove_Filtered_sessionStorage_And_localStorage3" width="650" height="266" class="alignnone size-full wp-image-8581" srcset="/wp-content/uploads/2015/08/Remove_Filtered_sessionStorage_And_localStorage3-e1438859851133.png 650w, /wp-content/uploads/2015/08/Remove_Filtered_sessionStorage_And_localStorage3-e1438859851133-300x123.png 300w, /wp-content/uploads/2015/08/Remove_Filtered_sessionStorage_And_localStorage3-e1438859851133-400x164.png 400w" sizes="(max-width: 650px) 100vw, 650px" /></a></p>
<p>Once you clicked, you can see all of your local storage items have been removed.</p>
<p><a href="http://sibeeshpassion.com/wp-content/uploads/2015/08/Remove_Filtered_sessionStorage_And_localStorage4-e1438859935922.png"><img decoding="async" src="http://sibeeshpassion.com/wp-content/uploads/2015/08/Remove_Filtered_sessionStorage_And_localStorage4-e1438859935922.png" alt="Remove_Filtered_sessionStorage_And_localStorage4" width="650" height="102" class="alignnone size-full wp-image-8591" srcset="/wp-content/uploads/2015/08/Remove_Filtered_sessionStorage_And_localStorage4-e1438859935922.png 650w, /wp-content/uploads/2015/08/Remove_Filtered_sessionStorage_And_localStorage4-e1438859935922-300x47.png 300w, /wp-content/uploads/2015/08/Remove_Filtered_sessionStorage_And_localStorage4-e1438859935922-400x63.png 400w" sizes="(max-width: 650px) 100vw, 650px" /></a></p>
<p>Now we will reload the page and set the storage items again, this time we will fire the event which delete the local storage value which has key starts with &#8220;First&#8221;.</p>
<p><a href="http://sibeeshpassion.com/wp-content/uploads/2015/08/Remove_Filtered_sessionStorage_And_localStorage5-e1438860090376.png"><img decoding="async" src="http://sibeeshpassion.com/wp-content/uploads/2015/08/Remove_Filtered_sessionStorage_And_localStorage5-e1438860090376.png" alt="Remove_Filtered_sessionStorage_And_localStorage5" width="650" height="264" class="alignnone size-full wp-image-8601" srcset="/wp-content/uploads/2015/08/Remove_Filtered_sessionStorage_And_localStorage5-e1438860090376.png 650w, /wp-content/uploads/2015/08/Remove_Filtered_sessionStorage_And_localStorage5-e1438860090376-300x122.png 300w, /wp-content/uploads/2015/08/Remove_Filtered_sessionStorage_And_localStorage5-e1438860090376-400x162.png 400w" sizes="(max-width: 650px) 100vw, 650px" /></a></p>
<p>Once you clicked you can see only the storage element which has key starts with &#8220;First&#8221; has been removed.</p>
<p><a href="http://sibeeshpassion.com/wp-content/uploads/2015/08/Remove_Filtered_sessionStorage_And_localStorage6-e1438860182653.png"><img decoding="async" src="http://sibeeshpassion.com/wp-content/uploads/2015/08/Remove_Filtered_sessionStorage_And_localStorage6-e1438860182653.png" alt="Remove_Filtered_sessionStorage_And_localStorage6" width="650" height="101" class="alignnone size-full wp-image-8611" srcset="/wp-content/uploads/2015/08/Remove_Filtered_sessionStorage_And_localStorage6-e1438860182653.png 650w, /wp-content/uploads/2015/08/Remove_Filtered_sessionStorage_And_localStorage6-e1438860182653-300x47.png 300w, /wp-content/uploads/2015/08/Remove_Filtered_sessionStorage_And_localStorage6-e1438860182653-400x62.png 400w" sizes="(max-width: 650px) 100vw, 650px" /></a></p>
<p>Next, we will fire the click event which removes all the session storage. ok? </p>
<p><a href="http://sibeeshpassion.com/wp-content/uploads/2015/08/Remove_Filtered_sessionStorage_And_localStorage7-e1438860272461.png"><img decoding="async" src="http://sibeeshpassion.com/wp-content/uploads/2015/08/Remove_Filtered_sessionStorage_And_localStorage7-e1438860272461.png" alt="Remove_Filtered_sessionStorage_And_localStorage7" width="650" height="285" class="alignnone size-full wp-image-8621" srcset="/wp-content/uploads/2015/08/Remove_Filtered_sessionStorage_And_localStorage7-e1438860272461.png 650w, /wp-content/uploads/2015/08/Remove_Filtered_sessionStorage_And_localStorage7-e1438860272461-300x132.png 300w, /wp-content/uploads/2015/08/Remove_Filtered_sessionStorage_And_localStorage7-e1438860272461-400x175.png 400w" sizes="(max-width: 650px) 100vw, 650px" /></a></p>
<p>Once you clicked, you can see all of your session storage items have been removed.</p>
<p><a href="http://sibeeshpassion.com/wp-content/uploads/2015/08/Remove_Filtered_sessionStorage_And_localStorage8-e1438860363488.png"><img decoding="async" src="http://sibeeshpassion.com/wp-content/uploads/2015/08/Remove_Filtered_sessionStorage_And_localStorage8-e1438860363488.png" alt="Remove_Filtered_sessionStorage_And_localStorage8" width="650" height="121" class="alignnone size-full wp-image-8631" srcset="/wp-content/uploads/2015/08/Remove_Filtered_sessionStorage_And_localStorage8-e1438860363488.png 650w, /wp-content/uploads/2015/08/Remove_Filtered_sessionStorage_And_localStorage8-e1438860363488-300x56.png 300w, /wp-content/uploads/2015/08/Remove_Filtered_sessionStorage_And_localStorage8-e1438860363488-400x74.png 400w" sizes="(max-width: 650px) 100vw, 650px" /></a></p>
<p>Now we will reload the page and set the storage items again, this time we will fire the event which deletes the session storage value which has key starts with &#8220;First&#8221;.</p>
<p><a href="http://sibeeshpassion.com/wp-content/uploads/2015/08/Remove_Filtered_sessionStorage_And_localStorage9-e1438860452170.png"><img decoding="async" src="http://sibeeshpassion.com/wp-content/uploads/2015/08/Remove_Filtered_sessionStorage_And_localStorage9-e1438860452170.png" alt="Remove_Filtered_sessionStorage_And_localStorage9" width="650" height="280" class="alignnone size-full wp-image-8641" srcset="/wp-content/uploads/2015/08/Remove_Filtered_sessionStorage_And_localStorage9-e1438860452170.png 650w, /wp-content/uploads/2015/08/Remove_Filtered_sessionStorage_And_localStorage9-e1438860452170-300x129.png 300w, /wp-content/uploads/2015/08/Remove_Filtered_sessionStorage_And_localStorage9-e1438860452170-400x172.png 400w" sizes="(max-width: 650px) 100vw, 650px" /></a></p>
<p>Once you clicked you can see only the session storage element which has key starts with &#8220;First&#8221; has been removed.</p>
<p><a href="http://sibeeshpassion.com/wp-content/uploads/2015/08/Remove_Filtered_sessionStorage_And_localStorage10-e1438860535759.png"><img decoding="async" src="http://sibeeshpassion.com/wp-content/uploads/2015/08/Remove_Filtered_sessionStorage_And_localStorage10-e1438860535759.png" alt="Remove_Filtered_sessionStorage_And_localStorage10" width="650" height="126" class="alignnone size-full wp-image-8651" srcset="/wp-content/uploads/2015/08/Remove_Filtered_sessionStorage_And_localStorage10-e1438860535759.png 650w, /wp-content/uploads/2015/08/Remove_Filtered_sessionStorage_And_localStorage10-e1438860535759-300x58.png 300w, /wp-content/uploads/2015/08/Remove_Filtered_sessionStorage_And_localStorage10-e1438860535759-400x78.png 400w" sizes="(max-width: 650px) 100vw, 650px" /></a></p>
<p>So we have done everything.
</p>
<h3>Complete Code</h3>
<p>
[html]<br />
&lt;html&gt;<br />
   &lt;head&gt;<br />
      &lt;title&gt;Remove all session storage,local storage or remove by key which starts with a particular string &#8211; Sibeesh Passion&lt;/title&gt;<br />
      &lt;script src=&quot;https://code.jquery.com/jquery-2.1.4.min.js&quot;&gt;&lt;/script&gt;<br />
   &lt;/head&gt;<br />
   &lt;body&gt;<br />
   &lt;a href=&quot;#&quot; id=&quot;removeAllLocalStorage&quot;&gt;Click To Remove All Local Storage&lt;/a&gt;&lt;br/&gt;&lt;br/&gt;<br />
   &lt;a href=&quot;#&quot; id=&quot;removeAllLocalStorageWhichStarts&quot;&gt;Click To Remove All Local Storage Which Starts With &quot;First&quot;&lt;/a&gt;&lt;br/&gt;&lt;br/&gt;<br />
   &lt;a href=&quot;#&quot; id=&quot;removeAllSessionStorage&quot;&gt;Click To Remove All Session Storage&lt;/a&gt;&lt;br/&gt;&lt;br/&gt;<br />
   &lt;a href=&quot;#&quot; id=&quot;removeAllSessionStorageWhichStarts&quot;&gt;Click To Remove All Session Storage Which Starts With &quot;First&quot;&lt;/a&gt;&lt;br/&gt;&lt;br/&gt;<br />
   &lt;div id=&quot;body&quot;&gt;&lt;/div&gt;<br />
      &lt;script&gt;<br />
       $(document).ready(function(){<br />
			localStorage.setItem(&quot;First1&quot;,&quot;First Local Storage&quot;);<br />
			sessionStorage.setItem(&quot;First1&quot;,&quot;First Session Storage&quot;);<br />
			localStorage.setItem(&quot;Second1&quot;,&quot;Second Local Storage&quot;);<br />
			sessionStorage.setItem(&quot;Second1&quot;,&quot;Second Session Storage&quot;);</p>
<p>            $(&quot;#removeAllLocalStorage&quot;).click(function(){<br />
				Object.keys(localStorage)<br />
				.forEach(function (key) {<br />
						localStorage.removeItem(key);<br />
				});<br />
            });<br />
			$(&quot;#removeAllLocalStorageWhichStarts&quot;).click(function(){<br />
				Object.keys(localStorage)<br />
				.forEach(function (key) {<br />
						if ((/^First/.test(key))) {<br />
							localStorage.removeItem(key);<br />
						}<br />
				});<br />
            });<br />
			$(&quot;#removeAllSessionStorage&quot;).click(function(){<br />
				Object.keys(sessionStorage)<br />
				.forEach(function (key) {<br />
						sessionStorage.removeItem(key);<br />
				});<br />
            });<br />
			$(&quot;#removeAllSessionStorageWhichStarts&quot;).click(function(){<br />
				Object.keys(sessionStorage)<br />
				.forEach(function (key) {<br />
						if ((/^First/.test(key))) {<br />
							sessionStorage.removeItem(key);<br />
						}<br />
				});<br />
            });<br />
       });<br />
      &lt;/script&gt;</p>
<p>   &lt;/body&gt;<br />
&lt;/html&gt;<br />
[/html]
</p>
<h3>Conclusion</h3>
<p>
Thanks a lot for reading. Did I miss anything that you may think which is needed? Could you find this post as useful? I hope you liked this article. Please share me your valuable suggestions and feedback.
</p>
<h3>Your turn. What do you think?</h3>
<p>
A blog isn’t a blog without comments, but do try to stay on topic. If you have a question unrelated to this post, you’re better off posting it on C# Corner, Code Project, Stack Overflow, Asp.Net Forum instead of commenting here. Tweet or email me a link to your question there and I’ll definitely try to help if I can.
</p>
<p>Kindest Regards<br />
Sibeesh Venu</p>
]]></content:encoded>
					
					<wfw:commentRss>https://mail.sibeeshpassion.com/remove-filtered-sessionstorage-and-localstorage/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Working With Client Side Local Storage</title>
		<link>https://mail.sibeeshpassion.com/working-with-client-side-local-storage/</link>
					<comments>https://mail.sibeeshpassion.com/working-with-client-side-local-storage/#disqus_thread</comments>
		
		<dc:creator><![CDATA[SibeeshVenu]]></dc:creator>
		<pubDate>Mon, 03 Aug 2015 00:09:38 +0000</pubDate>
				<category><![CDATA[CodeProject]]></category>
		<category><![CDATA[HTML5]]></category>
		<category><![CDATA[Local Storage]]></category>
		<category><![CDATA[Storage in HTML5]]></category>
		<guid isPermaLink="false">https://sibeeshpassion.com/?p=1411</guid>

					<description><![CDATA[[toc] Introduction In this article, we will learn how to use local storage in HTML5. For many years, we developers were depending on the server only for saving the data. And we used to retrieve the data by sending a request to our server and by manipulating some queries we could manage the formation, updating, insertion and so on. Great, we were doing a fantastic job. But as the day’s passes, we must move on to the next level. Nowadays all are busy in their life, so a person can wait a maximum of a few seconds for a request [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>[toc]</p>
<h3>Introduction</h3>
<p>
In this article, we will learn how to use local storage in HTML5. For many years, we developers were depending on the server only for saving the data. And we used to retrieve the data by sending a request to our server and by manipulating some queries we could manage the formation, updating, insertion and so on. Great, we were doing a fantastic job.</p>
<p>But as the day’s passes, we must move on to the next level. Nowadays all are busy in their life, so a person can wait a maximum of a few seconds for a request that he has given in your blog, or website, or in your product. If it takes much time, he/she will just close the window and start searching for an alternative. Am I right?</p>
<p>Yeah, it happens when you are depending completely on the server. For each query, it will take some seconds. Just because of this, we started using the client-side state management techniques, mostly cookies.</p>
<p>But we do have the following issues if we are using cookies also:</p>
<p>The cookies can have only a limited amount of data, which is definitely not enough for us to work on (4096 bytes).<br />
And as the number of requests increases, the data amount in cookies also increase. So at a point in time, we may encounter some performance issues. So in the modern web, we have a new solution, Local Storage.
</p>
<h3>Source Code</h3>
<p>Download the source code here: <a href="https://code.msdn.microsoft.com/Client-Side-Local-Storage-1db5f707" target="_blank">Local Storage </a></p>
<h3>Background</h3>
<p>Currently, I am working on a dashboard application. As you all know a dashboard application must be as fast as possible. It must not make a user wait. So most of our codes are on the client side. So we are storing the necessary data on the client side itself, that makes my app fast. And we are using jQuery.</p>
<h3>Using the code</h3>
<p>Now we will work on how to save the data in local storage and retrieve it when we need it.</p>
<h4>Saving the Data</h4>
<p>To save the data we do have a function called <em>setItem().</em> We can use the following syntax.</p>
<p>[js]<br />
localStorage.setItem(“item”, “http://sibeeshpassion.com/”)<br />
[/js]</p>
<p>Before going for this, you must check that your browser supports this feature since this is only applicable to modern browsers.</p>
<h4>To check whether it supports it or not:</h4>
<p>[js]<br />
if (localStorage) {<br />
}<br />
else {<br />
$(“#showitem”).text(“your browser does not support this feature”);<br />
}<br />
[/js]</p>
<h4>Retrieving the data from local storage</h4>
<p>We can use the <em>getItem() </em>function to retrieve the data from the local storage.</p>
<p>Please see the following syntax.<br />
[js]<br />
var itm = localStorage.getItem(“item”);<br />
[/js]</p>
<p>Great, you have it. Congratulations.</p>
<p>Now we will work on how to remove this item from local storage.</p>
<h4>Removing data from the local storage</h4>
<p>To remove the data form the local storage, please see the following syntax.<br />
[js]<br />
localStorage.removeItem(“item”);<br />
[/js]</p>
<h4>Removing the all data from localStorage</h4>
<p>To remove the data form the localStorage, please see the following syntax.<br />
[js]<br />
localStorage.clear();<br />
[/js]</p>
<h3>Demo</h3>
<p>Here we will create 3 buttons and one h2 tag as in the following markup.</p>
<p>[html]<br />
&lt;input type=“submit” id=“setitem” value=“set Item” /&gt;<br />
&lt;input type=“submit” id=“getitem” value=“get Item” /&gt;<br />
&lt;input type=“submit” id=“removeitem” value=“remove Item” /&gt;<br />
&lt;h2 id=“showitem”&gt;&lt;/h2&gt;<br />
[/html]</p>
<p>Now we will add the jQuery reference.</p>
<p>[js]<br />
&lt;script src=“jquery-1.11.1.min.js”&gt;&lt;/script&gt;<br />
[/js]</p>
<h4>Set the Item</h4>
<p>[js]<br />
$(“#setitem”).click(function () {<br />
localStorage.setItem(“item”, “http://sibeeshpassion.com/”)<br />
});<br />
[/js]</p>
<p>Once you set the item to localStorage, you can see it in your browser. To see it, press F12 after pressing the set Item button, then go to the resources tab.</p>
<p>Figure: Prior to setting the item</p>
<p><a href="http://sibeeshpassion.com/wp-content/uploads/2015/03/set_items_to_local_storage.jpg"><img decoding="async" src="http://sibeeshpassion.com/wp-content/uploads/2015/03/set_items_to_local_storage.jpg" alt="set_items_to_local_storage" width="611" height="265" class="alignnone size-full wp-image-8501" srcset="/wp-content/uploads/2015/03/set_items_to_local_storage.jpg 611w, /wp-content/uploads/2015/03/set_items_to_local_storage-300x130.jpg 300w, /wp-content/uploads/2015/03/set_items_to_local_storage-400x173.jpg 400w" sizes="(max-width: 611px) 100vw, 611px" /></a></p>
<p>Figure: After setting the item</p>
<p><a href="http://sibeeshpassion.com/wp-content/uploads/2015/03/set_items_to_local_storage1.jpg"><img decoding="async" src="http://sibeeshpassion.com/wp-content/uploads/2015/03/set_items_to_local_storage1.jpg" alt="set_items_to_local_storage1" width="692" height="297" class="alignnone size-full wp-image-8511" srcset="/wp-content/uploads/2015/03/set_items_to_local_storage1.jpg 634w, /wp-content/uploads/2015/03/set_items_to_local_storage1-300x129.jpg 300w, /wp-content/uploads/2015/03/set_items_to_local_storage1-400x172.jpg 400w" sizes="(max-width: 692px) 100vw, 692px" /></a></p>
<h4>Get the Item</h4>
<p>[js]<br />
$(“#getitem”).click(function () {<br />
                    var itm = localStorage.getItem(“item”)<br />
                    $(“#showitem”).text(itm);<br />
                });<br />
[/js]</p>
<h4>Remove the Item</h4>
<p>[js]<br />
$(“#removeitem”).click(function () {<br />
                    localStorage.removeItem(“item”);<br />
                    $(“#showitem”).text(”);<br />
                });<br />
[/js]</p>
<h3>Complete Code</h3>
<p>[html]<br />
&lt;!DOCTYPE html&gt;<br />
&lt;html xmlns=“http://www.w3.org/1999/xhtml”&gt;<br />
&lt;head&gt;<br />
    &lt;title&gt;Use Of Local Storage – Sibeesh|Passion&lt;/title&gt;<br />
    &lt;script src=“jquery-1.11.1.min.js”&gt;&lt;/script&gt;<br />
    &lt;script&gt;<br />
        $(function () {<br />
            if (localStorage) {<br />
                $(“#setitem”).click(function () {<br />
                    localStorage.setItem(“item”, “http://sibeeshpassion.com/”)<br />
                });<br />
                $(“#getitem”).click(function () {<br />
                    var itm = localStorage.getItem(“item”)<br />
                    $(“#showitem”).text(itm);<br />
                });<br />
                $(“#removeitem”).click(function () {<br />
                    localStorage.removeItem(“item”);<br />
                    $(“#showitem”).text(”);<br />
                });<br />
            }<br />
            else {<br />
                $(“#showitem”).text(“your browser does not support this feature”);<br />
            }<br />
        });<br />
&lt;/script&gt;<br />
&lt;/head&gt;<br />
&lt;body&gt;<br />
    &lt;input type=“submit” id=“setitem” value=“set Item” /&gt;<br />
    &lt;input type=“submit” id=“getitem” value=“get Item” /&gt;<br />
    &lt;input type=“submit” id=“removeitem” value=“remove Item” /&gt;<br />
    &lt;h2 id=“showitem”&gt;&lt;/h2&gt;<br />
&lt;/body&gt;<br />
&lt;/html&gt;<br />
[/html]</p>
<h3>Conclusion</h3>
<p>Thanks a lot for reading. Did I miss anything that you may think which is needed? Could you find this post as useful? I hope you liked this article. Please share me your valuable suggestions and feedback.</p>
<h3>Your turn. What do you think?</h3>
<p>A blog isn’t a blog without comments, but do try to stay on topic. If you have a question unrelated to this post, you’re better off posting it on C# Corner, Code Project, Stack Overflow, Asp.Net Forum instead of commenting here. Tweet or email me a link to your question there and I’ll definitely try to help if I can.</p>
<p>Kindest Regards<br />
Sibeesh Venu</p>
]]></content:encoded>
					
					<wfw:commentRss>https://mail.sibeeshpassion.com/working-with-client-side-local-storage/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Basic Difference Between Local Storage and Session Storage in HTML 5</title>
		<link>https://mail.sibeeshpassion.com/basic-difference-between-local-storage-and-session-storage-in-html-5/</link>
					<comments>https://mail.sibeeshpassion.com/basic-difference-between-local-storage-and-session-storage-in-html-5/#disqus_thread</comments>
		
		<dc:creator><![CDATA[SibeeshVenu]]></dc:creator>
		<pubDate>Sun, 29 Mar 2015 20:14:03 +0000</pubDate>
				<category><![CDATA[HTML5]]></category>
		<category><![CDATA[Difference between local storage and session storage]]></category>
		<category><![CDATA[Local Storage]]></category>
		<category><![CDATA[Session Storage]]></category>
		<category><![CDATA[Storage in HTML5]]></category>
		<guid isPermaLink="false">https://sibeeshpassion.com/?p=1501</guid>

					<description><![CDATA[[toc] Introduction This article explains what is the basic difference between HTML 5 local storage and session storage. Basically, both session storage and local storage are part of storage mechanism which is introduced with HTML5. By using these two, we can store information on client side itself. I hope you will like it. If you are new to the term local storage, I recommend you to read my article here: Local Storage Basic scenario where you can use If you need to carry a value throughout your application or throughout your pages, you can use local storage. For example, we [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>[toc]</p>
<h3>Introduction</h3>
<p>
This article explains what is the basic difference between HTML 5 local storage and session storage. Basically, both session storage and local storage are part of storage mechanism which is introduced with HTML5. By using these two, we can store information on client side itself.  I hope you will like it.</p>
<p>If you are new to the term local storage, I recommend you to read my article here: <a href="http://sibeeshpassion.com/working-with-client-side-local-storage/" target="_blank">Local Storage</a></p>
<p><h3>Basic scenario where you can use</h3>
<p>
If you need to carry a value throughout your application or throughout your pages, you can use local storage. For example, we can store a logged-in username in local storage that we may need to access all the pages. I strongly recommend to not store any secured data (for example a “password”) in local storage.</p>
<p>In some scenario, we may need to set a key element depending on the pages we have in the application. For example, if we need to store the page name (any key element that may be different).<br />
So let’s start</p>
<p><h3>Using the code</h3>
<p>
Here in my application, I have added two HTML5 pages.</p>
<p><em>HtmlPage1.html</em><br />
<em>HtmlPage2.html</em></p>
<p>In <em>HtmlPage1.html</em> I have set local storage and session storage, now I am trying to access that in <em>HtmlPage2.html</em>. Please see the following source.</p>
<p><em>HtmlPage1.html</em><br />
[html]<br />
&lt;!DOCTYPE html&gt;<br />
&lt;html<br />
    xmlns=“http://www.w3.org/1999/xhtml”&gt;<br />
    &lt;head&gt;<br />
        &lt;title&gt;Difference between local storage and session storage&lt;/title&gt;<br />
        &lt;script&gt;<br />
            localStorage.setItem(“myKey”, 1);<br />
            sessionStorage.setItem(“myKey”, 1);<br />
            var myVal = localStorage.getItem(“myKey”);<br />
            alert(“My local storage value is : ” + myVal);<br />
            alert(“My session storage value is : ” + sessionStorage.getItem(“myKey”));<br />
        &lt;/script&gt;<br />
    &lt;/head&gt;<br />
    &lt;body&gt;<br />
    &lt;/body&gt;<br />
&lt;/html&gt;<br />
[/html]</p>
<p><em>HtmlPage2.html</em><br />
[html]<br />
&lt;!DOCTYPE html&gt;<br />
&lt;html<br />
    xmlns=“http://www.w3.org/1999/xhtml”&gt;<br />
    &lt;head&gt;<br />
        &lt;title&gt;Difference between local storage and session storage&lt;/title&gt;<br />
        &lt;script&gt;<br />
            var myVal = localStorage.getItem(“myKey”);<br />
            alert(“My local storage value is : ” + myVal);<br />
            alert(“My session storage value is : ” + sessionStorage.getItem(“myKey”));<br />
        &lt;/script&gt;<br />
    &lt;/head&gt;<br />
    &lt;body&gt;<br />
    &lt;/body&gt;<br />
&lt;/html&gt;<br />
[/html]</p>
<p>When you run the first page, in the browser console you will see as in the following image.</p>
<p><a href="http://sibeeshpassion.com/wp-content/uploads/2015/03/local-storage.jpg"><img decoding="async" src="http://sibeeshpassion.com/wp-content/uploads/2015/03/local-storage.jpg" alt="local storage" width="624" height="135" class="alignnone size-full wp-image-8461" srcset="/wp-content/uploads/2015/03/local-storage.jpg 624w, /wp-content/uploads/2015/03/local-storage-300x65.jpg 300w, /wp-content/uploads/2015/03/local-storage-620x135.jpg 620w, /wp-content/uploads/2015/03/local-storage-400x87.jpg 400w" sizes="(max-width: 624px) 100vw, 624px" /></a></p>
<p>Figure: Local Storage</p>
<p><a href="http://sibeeshpassion.com/wp-content/uploads/2015/03/session-storage.jpg"><img decoding="async" src="http://sibeeshpassion.com/wp-content/uploads/2015/03/session-storage.jpg" alt="session storage" width="624" height="135" class="alignnone size-full wp-image-8462" srcset="/wp-content/uploads/2015/03/session-storage.jpg 624w, /wp-content/uploads/2015/03/session-storage-300x65.jpg 300w, /wp-content/uploads/2015/03/session-storage-620x135.jpg 620w, /wp-content/uploads/2015/03/session-storage-400x87.jpg 400w" sizes="(max-width: 624px) 100vw, 624px" /></a></p>
<p>Figure: Session Storage</p>
<p>Now it is time to run the second page. Once you run that please check in the console. You can see as follows.</p>
<p><a href="http://sibeeshpassion.com/wp-content/uploads/2015/03/htmlpage2-local-storage.jpg"><img decoding="async" src="http://sibeeshpassion.com/wp-content/uploads/2015/03/htmlpage2-local-storage.jpg" alt="htmlpage2 local storage" width="624" height="301" class="alignnone size-full wp-image-8471" srcset="/wp-content/uploads/2015/03/htmlpage2-local-storage.jpg 624w, /wp-content/uploads/2015/03/htmlpage2-local-storage-300x145.jpg 300w, /wp-content/uploads/2015/03/htmlpage2-local-storage-400x193.jpg 400w" sizes="(max-width: 624px) 100vw, 624px" /></a></p>
<p>Figure: Local storage</p>
<p><a href="http://sibeeshpassion.com/wp-content/uploads/2015/03/htmlpage2-session-storage.jpg"><img decoding="async" src="http://sibeeshpassion.com/wp-content/uploads/2015/03/htmlpage2-session-storage.jpg" alt="htmlpage2 session storage" width="624" height="307" class="alignnone size-full wp-image-8481" srcset="/wp-content/uploads/2015/03/htmlpage2-session-storage.jpg 624w, /wp-content/uploads/2015/03/htmlpage2-session-storage-300x148.jpg 300w, /wp-content/uploads/2015/03/htmlpage2-session-storage-400x197.jpg 400w" sizes="(max-width: 624px) 100vw, 624px" /></a></p>
<p>Figure: Session storage</p>
<p>Please note that session storage is null. You can see the session storage only if you set it on that running page.
</p>
<h3>Conclusion</h3>
<p>
Thanks a lot for reading. Did I miss anything that you may think which is needed? Could you find this post as useful? I hope you liked this article. Please share me your valuable suggestions and feedback.
</p>
<h3>Your turn. What do you think?</h3>
<p>
A blog isn’t a blog without comments, but do try to stay on topic. If you have a question unrelated to this post, you’re better off posting it on C# Corner, Code Project, Stack Overflow, Asp.Net Forum instead of commenting here. Tweet or email me a link to your question there and I’ll definitely try to help if I can.
</p>
<p>Kindest Regards<br />
Sibeesh Venu</p>
]]></content:encoded>
					
					<wfw:commentRss>https://mail.sibeeshpassion.com/basic-difference-between-local-storage-and-session-storage-in-html-5/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
