SlideShare a Scribd company logo
HTML5 Web Storage
(DOM storage)




                    ©Inbal Geffen 2012
Why?
●   HTTP is stateless

●   We want to keep record of what the user did

●   We want to be able to work "offline"

●   We don't want to force users to signup / login




                                                     ©Inbal Geffen 2012
Cookies
●   Used before HTML5 Web Storage

●   4KB memory limitation per cookie

●   Sent in every request

●   Have a "bad reputation"




                                       ©Inbal Geffen 2012
localStorage vs.
sessionStorage
In Both
●   Data is stored as key/value pairs

●   Data is stored in string form

●   Use the same API : setItem(), getItem(), clear(), removeItem()

●   Fire 'storage' event




                                                                 ©Inbal Geffen 2012
localStorage vs.
sessionStorage
Differ in scope and lifetime
●   sessionStorage is stored until the window is closed

●   localStorage is stored until the storage is cleared

●   localStorage is synchronized within the browser windows and tabs

●   sessionStorage - multiple instances of the same window without collision




                                                                ©Inbal Geffen 2012
Web Storage Support
●   We must remember that not all browsers support "Web Storage"




function checkStorageSupport() {
        if (!window.localStorage) {
        alert('This browser does NOT support localStorage');
        }
   }




                                                               ©Inbal Geffen 2012
Web Storage API
setItem
//set Item in local storage
localStorage.setItem("userName", userName);

//can also use immediate set, but this is less recommended
localStorage.userName=userName;



//set Item in session storage
sessionStorage.setItem("userName", userName);

//can also use the immediate set, but this is less recommended
sessionStorage.userName=userName;


                                                                 ©Inbal Geffen 2012
Web Storage API
getItem
//get Item in local storage
var userName = localStorage.getItem("userName);

//can also use immediate get, but this is less recommended
var userName = localStorage.userName;



//get Item in session storage
sessionStorage.getItem("userName);

//can also use the immediate set, but this is less recommended
var userName = sessionStorage.userName;


                                                                 ©Inbal Geffen 2012
Web Storage API
clear(), removeItem
//clear the local storage
localStorage.clear();

//remove specific item from local storage
localStorage.removeItem("userName");
//localStorage.getItem("userName") => NULL

//clear the session storage
sessionStorage.clear();

//remove specific item from session storage
sessionStorage.removeItem("userName");



                                              ©Inbal Geffen 2012
Web Storage API

●   Web Storage is an array

●   localStorage.length

●   Item in the ith position in the array : localStorage.key(i)




                                                                  ©Inbal Geffen 2012
Storage Event
//Fired when performing an operation on the storage


if (window.addEventListener) {
    window.addEventListener("storage", handleStorageEvent, false);
} else {
    // for IE versions below IE9
    window.attachEvent("onstorage", handleStorageEvent);
};

function handleStorageEvent(eventData) {
  // Do something
}



                                                                 ©Inbal Geffen 2012
Things to remember
• Local storage persists until it is deleted or the browser’s cache is cleared.

• Session storage persists until it is deleted or the browsing context is closed.

• Data stored by one browser is not accessible by another browser.
  For example, data stored by Chrome is not seen by Firefox.

• Objects should be stored as strings.

• For security reasons, sensitive data should not be stored, especially in local
  storage.

• Changes to a storage area cause a “storage” event to be fired.

• As with many other HTML5 features, web storage is not yet implemented
  consistently.


                                                                       ©Inbal Geffen 2012
HTML5 Web Workers




                    ©Inbal Geffen 2012
THE PROBLEM:
 JAVASCRIPT CONCURRENCY
• JavaScript is a single-threaded environment

• Used to be "solved" with asynchronous techniques such as:
  setTimeout(), setInterval(), XMLHttpRequest, and event handlers

• Asynchronous events are processed after the current executing script

• Web Workers are the HTML5 solution, enabling multi threading




                                                                    ©Inbal Geffen 2012
Web Workers - Use Cases

Doing an action/process on the background, without harming the UI
Show something to the user and then we can update the UI with the result.

●   Updating many rows of local web database

●   Processing large arrays

●   Background I/O - fetch data for later

●   Spell checker

●   Code syntax highlighting or other real-time text formatting




                                                                     ©Inbal Geffen 2012
Web Workers Support
●   We need to remember to check browser support for web workers

function checkWorkerSupport() {
         if (typeof(window.Worker) === "undefined")
         alert("Your browser doesn't support HTML5 Web Workers!");
    }




                                                               ©Inbal Geffen 2012
Create Web Worker - 1
●   Web workers run from an external JS file
    (We will use a file called primesWorker.js as an example)

●   Web workers will be called from our HTML file

●   So we need two files : our HTML file and a JS file

●   Communication is done using messages : postMessage()

●   Ths JS file will have the function we would like to run on a different thread

●   The HTML file will:
     ○ Call the Web Worker (using javascript)
     ○ Respond to the Web Worker's messages
     ○ Change the UI



                                                                     ©Inbal Geffen 2012
Create Web Worker - 2
Main HTML file - create web worker

●   Create a new instance of web worker
    The worker gets the file name as a parameter
    var worker = new Worker("primesWorker.js");

●   If the file exists, a new thread will be asynchronously created

●   Calling the worker: postMessage()
    worker.postMessage(100);

●   postMessage() can get one parameter

●   This is the parameter that will be sent to the worker

●   So we see we can send messages to the worker from the HTML file

                                                                      ©Inbal Geffen 2012
Create Web Worker - 3
Main HTML file - get info from web worker

●   Getting messages FROM the worker


●   We need to listen to the 'message' event

worker.onmessage = function (e) {
        //do something with the message we got from the worker
        }




                                                                 ©Inbal Geffen 2012
Create Web Worker - 4
Main HTML file - errors

●   Check for errors

// Show errors from the worker
        worker.onerror = function (error) {
        alert(error.data);
        }




                                              ©Inbal Geffen 2012
Features Available to Workers
Due to their multi-threaded behavior, web workers only has access to a subset
of JavaScript's features:

 ●   The navigator object

 ●   The location object (contains information about the current URL)

 ●   XMLHttpRequest

 ●   setTimeout()/clearTimeout() and setInterval()/clearInterval()

 ●   Importing external scripts using the importScripts() method

 ●   Spawning other web workers


                                                                     ©Inbal Geffen 2012
Workers do NOT have access
 ●   The DOM (it's not thread-safe)

 ●   The window object

 ●   The document object

 ●   The parent object


That's why we need to communicate using messages.




                                                    ©Inbal Geffen 2012

More Related Content

PPTX
ReactJS presentation.pptx
DivyanshGupta922023
 
PPTX
Introduction to React JS
Arnold Asllani
 
PPTX
Presentation of bootstrap
1amitgupta
 
PDF
Introduction to HTML-CSS-Javascript.pdf
DakshPratapSingh1
 
PPTX
[Final] ReactJS presentation
洪 鹏发
 
PPTX
Introduction to ASP.NET MVC
Khaled Musaied
 
PDF
Bootstrap 5 basic
Jubair Ahmed Junjun
 
PPTX
MERN PPT
NeerajGupta96647
 
ReactJS presentation.pptx
DivyanshGupta922023
 
Introduction to React JS
Arnold Asllani
 
Presentation of bootstrap
1amitgupta
 
Introduction to HTML-CSS-Javascript.pdf
DakshPratapSingh1
 
[Final] ReactJS presentation
洪 鹏发
 
Introduction to ASP.NET MVC
Khaled Musaied
 
Bootstrap 5 basic
Jubair Ahmed Junjun
 

What's hot (20)

PDF
ReactJS presentation
Thanh Tuong
 
PPTX
Express js
Manav Prasad
 
PDF
NodeJS for Beginner
Apaichon Punopas
 
PPTX
INDUSTRIAL TRAINING Presentation on Web Development. (2).pptx
12KritiGaneriwal
 
PPTX
NodeJS - Server Side JS
Ganesh Kondal
 
PDF
Nestjs MasterClass Slides
Nir Kaufman
 
PPTX
What Is Express JS?
Simplilearn
 
PPTX
Introduction to Bootstrap
Collaboration Technologies
 
PDF
MEAN Stack
Krishnaprasad k
 
PPTX
akshintern[3].pptx internshipp web development information technology
uniqueseller2002
 
PPTX
Front-end development introduction (HTML, CSS). Part 1
Oleksii Prohonnyi
 
PDF
Workshop 4: NodeJS. Express Framework & MongoDB.
Visual Engineering
 
PPTX
Introduction to angular with a simple but complete project
Jadson Santos
 
PPT
Php mysql ppt
Karmatechnologies Pvt. Ltd.
 
PDF
An Introduction of Node Package Manager (NPM)
iFour Technolab Pvt. Ltd.
 
PPTX
Css3
Deepak Mangal
 
PPTX
Introduction to ASP.NET
Rajkumarsoy
 
PPT
MYSQL
Ankush Jain
 
PPTX
Single page application
Arthur Fung
 
ReactJS presentation
Thanh Tuong
 
Express js
Manav Prasad
 
NodeJS for Beginner
Apaichon Punopas
 
INDUSTRIAL TRAINING Presentation on Web Development. (2).pptx
12KritiGaneriwal
 
NodeJS - Server Side JS
Ganesh Kondal
 
Nestjs MasterClass Slides
Nir Kaufman
 
What Is Express JS?
Simplilearn
 
Introduction to Bootstrap
Collaboration Technologies
 
MEAN Stack
Krishnaprasad k
 
akshintern[3].pptx internshipp web development information technology
uniqueseller2002
 
Front-end development introduction (HTML, CSS). Part 1
Oleksii Prohonnyi
 
Workshop 4: NodeJS. Express Framework & MongoDB.
Visual Engineering
 
Introduction to angular with a simple but complete project
Jadson Santos
 
An Introduction of Node Package Manager (NPM)
iFour Technolab Pvt. Ltd.
 
Introduction to ASP.NET
Rajkumarsoy
 
Single page application
Arthur Fung
 
Ad

Viewers also liked (10)

PDF
Html5 storage suggestions for challenges.pptx
deepmoteria
 
PDF
Local storage in Web apps
Ivano Malavolta
 
PDF
Best Web-based Data Visualization tools
Atchai
 
PPTX
HTML5 Local Storage
Lior Zamir
 
PDF
HTML5 Storage/Cache
Andy Wang
 
PDF
Html5 web storage
Mindfire Solutions
 
PDF
PyCon 2012: Python for data lovers: explore it, analyze it, map it
Jacqueline Kazil
 
PDF
Html5-Web-Storage
Mindfire Solutions
 
PDF
Personas: Understanding the User Behind the Visit
Michael King
 
PDF
Cloud storage slides
Evan Powell
 
Html5 storage suggestions for challenges.pptx
deepmoteria
 
Local storage in Web apps
Ivano Malavolta
 
Best Web-based Data Visualization tools
Atchai
 
HTML5 Local Storage
Lior Zamir
 
HTML5 Storage/Cache
Andy Wang
 
Html5 web storage
Mindfire Solutions
 
PyCon 2012: Python for data lovers: explore it, analyze it, map it
Jacqueline Kazil
 
Html5-Web-Storage
Mindfire Solutions
 
Personas: Understanding the User Behind the Visit
Michael King
 
Cloud storage slides
Evan Powell
 
Ad

Similar to Web Storage & Web Workers (20)

PPT
HTML5 Multithreading
Allan Huang
 
PPT
WP - Unit I.ppt
SATHYABAMAMADHANKUMA
 
PPTX
webworkers
Asanka Indrajith
 
PDF
2013 jsdc webworker
Bingo Yang
 
PDF
Web workers
Surbhi Mathur
 
PDF
Web workers
Surbhi Mathur
 
PDF
Workers of the web - BrazilJS 2013
Thibault Imbert
 
PPTX
Web worker
Nitin Giri
 
PPTX
Html web workers
AbhishekMondal42
 
PDF
HTML5: Building the Next Generation of Web Applications
Chrome Developer Relations
 
PDF
Html5 for beginners
n|u - The Open Security Community
 
PDF
Service worker API
Giorgio Natili
 
PDF
Should you use HTML5 to build your product? The pros & cons of using current ...
boxuno
 
PDF
Fast Cordova applications
Ivano Malavolta
 
PPTX
HTML5 Web storage
Muhammad Ehtisham Siddiqui
 
PDF
Web workers and service workers
Nitish Phanse
 
PPTX
Web workers | JavaScript | HTML API
pcnmtutorials
 
PDF
Future of Mobile Web - Coldfront conf
Paul Kinlan
 
PPT
Intro to Service Worker API and its use cases
satejsahu
 
PPTX
HTML5 on Mobile
Adam Lu
 
HTML5 Multithreading
Allan Huang
 
WP - Unit I.ppt
SATHYABAMAMADHANKUMA
 
webworkers
Asanka Indrajith
 
2013 jsdc webworker
Bingo Yang
 
Web workers
Surbhi Mathur
 
Web workers
Surbhi Mathur
 
Workers of the web - BrazilJS 2013
Thibault Imbert
 
Web worker
Nitin Giri
 
Html web workers
AbhishekMondal42
 
HTML5: Building the Next Generation of Web Applications
Chrome Developer Relations
 
Service worker API
Giorgio Natili
 
Should you use HTML5 to build your product? The pros & cons of using current ...
boxuno
 
Fast Cordova applications
Ivano Malavolta
 
HTML5 Web storage
Muhammad Ehtisham Siddiqui
 
Web workers and service workers
Nitish Phanse
 
Web workers | JavaScript | HTML API
pcnmtutorials
 
Future of Mobile Web - Coldfront conf
Paul Kinlan
 
Intro to Service Worker API and its use cases
satejsahu
 
HTML5 on Mobile
Adam Lu
 

More from Inbal Geffen (9)

PDF
Css3
Inbal Geffen
 
PDF
Jqeury ajax plugins
Inbal Geffen
 
PDF
Jquery mobile2
Inbal Geffen
 
PDF
Jquery2
Inbal Geffen
 
PDF
J querypractice
Inbal Geffen
 
PDF
J queryui
Inbal Geffen
 
PDF
Mysql & Php
Inbal Geffen
 
PDF
jQuery mobile UX
Inbal Geffen
 
PDF
Jqeury ajax plugins
Inbal Geffen
 
Jqeury ajax plugins
Inbal Geffen
 
Jquery mobile2
Inbal Geffen
 
Jquery2
Inbal Geffen
 
J querypractice
Inbal Geffen
 
J queryui
Inbal Geffen
 
Mysql & Php
Inbal Geffen
 
jQuery mobile UX
Inbal Geffen
 
Jqeury ajax plugins
Inbal Geffen
 

Recently uploaded (20)

PPTX
Simple and concise overview about Quantum computing..pptx
mughal641
 
PPTX
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
PPTX
Agile Chennai 18-19 July 2025 Ideathon | AI Powered Microfinance Literacy Gui...
AgileNetwork
 
PDF
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PDF
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
PDF
OFFOFFBOX™ – A New Era for African Film | Startup Presentation
ambaicciwalkerbrian
 
PDF
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
PDF
Doc9.....................................
SofiaCollazos
 
PDF
Software Development Methodologies in 2025
KodekX
 
PDF
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
PDF
Make GenAI investments go further with the Dell AI Factory
Principled Technologies
 
PDF
Economic Impact of Data Centres to the Malaysian Economy
flintglobalapac
 
PDF
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
PDF
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
PDF
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
PPTX
IT Runs Better with ThousandEyes AI-driven Assurance
ThousandEyes
 
PDF
Peak of Data & AI Encore - Real-Time Insights & Scalable Editing with ArcGIS
Safe Software
 
PPTX
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
PPTX
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
 
Simple and concise overview about Quantum computing..pptx
mughal641
 
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
Agile Chennai 18-19 July 2025 Ideathon | AI Powered Microfinance Literacy Gui...
AgileNetwork
 
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
OFFOFFBOX™ – A New Era for African Film | Startup Presentation
ambaicciwalkerbrian
 
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
Doc9.....................................
SofiaCollazos
 
Software Development Methodologies in 2025
KodekX
 
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
Make GenAI investments go further with the Dell AI Factory
Principled Technologies
 
Economic Impact of Data Centres to the Malaysian Economy
flintglobalapac
 
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
IT Runs Better with ThousandEyes AI-driven Assurance
ThousandEyes
 
Peak of Data & AI Encore - Real-Time Insights & Scalable Editing with ArcGIS
Safe Software
 
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
 

Web Storage & Web Workers

  • 1. HTML5 Web Storage (DOM storage) ©Inbal Geffen 2012
  • 2. Why? ● HTTP is stateless ● We want to keep record of what the user did ● We want to be able to work "offline" ● We don't want to force users to signup / login ©Inbal Geffen 2012
  • 3. Cookies ● Used before HTML5 Web Storage ● 4KB memory limitation per cookie ● Sent in every request ● Have a "bad reputation" ©Inbal Geffen 2012
  • 4. localStorage vs. sessionStorage In Both ● Data is stored as key/value pairs ● Data is stored in string form ● Use the same API : setItem(), getItem(), clear(), removeItem() ● Fire 'storage' event ©Inbal Geffen 2012
  • 5. localStorage vs. sessionStorage Differ in scope and lifetime ● sessionStorage is stored until the window is closed ● localStorage is stored until the storage is cleared ● localStorage is synchronized within the browser windows and tabs ● sessionStorage - multiple instances of the same window without collision ©Inbal Geffen 2012
  • 6. Web Storage Support ● We must remember that not all browsers support "Web Storage" function checkStorageSupport() { if (!window.localStorage) { alert('This browser does NOT support localStorage'); } } ©Inbal Geffen 2012
  • 7. Web Storage API setItem //set Item in local storage localStorage.setItem("userName", userName); //can also use immediate set, but this is less recommended localStorage.userName=userName; //set Item in session storage sessionStorage.setItem("userName", userName); //can also use the immediate set, but this is less recommended sessionStorage.userName=userName; ©Inbal Geffen 2012
  • 8. Web Storage API getItem //get Item in local storage var userName = localStorage.getItem("userName); //can also use immediate get, but this is less recommended var userName = localStorage.userName; //get Item in session storage sessionStorage.getItem("userName); //can also use the immediate set, but this is less recommended var userName = sessionStorage.userName; ©Inbal Geffen 2012
  • 9. Web Storage API clear(), removeItem //clear the local storage localStorage.clear(); //remove specific item from local storage localStorage.removeItem("userName"); //localStorage.getItem("userName") => NULL //clear the session storage sessionStorage.clear(); //remove specific item from session storage sessionStorage.removeItem("userName"); ©Inbal Geffen 2012
  • 10. Web Storage API ● Web Storage is an array ● localStorage.length ● Item in the ith position in the array : localStorage.key(i) ©Inbal Geffen 2012
  • 11. Storage Event //Fired when performing an operation on the storage if (window.addEventListener) { window.addEventListener("storage", handleStorageEvent, false); } else { // for IE versions below IE9 window.attachEvent("onstorage", handleStorageEvent); }; function handleStorageEvent(eventData) { // Do something } ©Inbal Geffen 2012
  • 12. Things to remember • Local storage persists until it is deleted or the browser’s cache is cleared. • Session storage persists until it is deleted or the browsing context is closed. • Data stored by one browser is not accessible by another browser. For example, data stored by Chrome is not seen by Firefox. • Objects should be stored as strings. • For security reasons, sensitive data should not be stored, especially in local storage. • Changes to a storage area cause a “storage” event to be fired. • As with many other HTML5 features, web storage is not yet implemented consistently. ©Inbal Geffen 2012
  • 13. HTML5 Web Workers ©Inbal Geffen 2012
  • 14. THE PROBLEM: JAVASCRIPT CONCURRENCY • JavaScript is a single-threaded environment • Used to be "solved" with asynchronous techniques such as: setTimeout(), setInterval(), XMLHttpRequest, and event handlers • Asynchronous events are processed after the current executing script • Web Workers are the HTML5 solution, enabling multi threading ©Inbal Geffen 2012
  • 15. Web Workers - Use Cases Doing an action/process on the background, without harming the UI Show something to the user and then we can update the UI with the result. ● Updating many rows of local web database ● Processing large arrays ● Background I/O - fetch data for later ● Spell checker ● Code syntax highlighting or other real-time text formatting ©Inbal Geffen 2012
  • 16. Web Workers Support ● We need to remember to check browser support for web workers function checkWorkerSupport() { if (typeof(window.Worker) === "undefined") alert("Your browser doesn't support HTML5 Web Workers!"); } ©Inbal Geffen 2012
  • 17. Create Web Worker - 1 ● Web workers run from an external JS file (We will use a file called primesWorker.js as an example) ● Web workers will be called from our HTML file ● So we need two files : our HTML file and a JS file ● Communication is done using messages : postMessage() ● Ths JS file will have the function we would like to run on a different thread ● The HTML file will: ○ Call the Web Worker (using javascript) ○ Respond to the Web Worker's messages ○ Change the UI ©Inbal Geffen 2012
  • 18. Create Web Worker - 2 Main HTML file - create web worker ● Create a new instance of web worker The worker gets the file name as a parameter var worker = new Worker("primesWorker.js"); ● If the file exists, a new thread will be asynchronously created ● Calling the worker: postMessage() worker.postMessage(100); ● postMessage() can get one parameter ● This is the parameter that will be sent to the worker ● So we see we can send messages to the worker from the HTML file ©Inbal Geffen 2012
  • 19. Create Web Worker - 3 Main HTML file - get info from web worker ● Getting messages FROM the worker ● We need to listen to the 'message' event worker.onmessage = function (e) { //do something with the message we got from the worker } ©Inbal Geffen 2012
  • 20. Create Web Worker - 4 Main HTML file - errors ● Check for errors // Show errors from the worker worker.onerror = function (error) { alert(error.data); } ©Inbal Geffen 2012
  • 21. Features Available to Workers Due to their multi-threaded behavior, web workers only has access to a subset of JavaScript's features: ● The navigator object ● The location object (contains information about the current URL) ● XMLHttpRequest ● setTimeout()/clearTimeout() and setInterval()/clearInterval() ● Importing external scripts using the importScripts() method ● Spawning other web workers ©Inbal Geffen 2012
  • 22. Workers do NOT have access ● The DOM (it's not thread-safe) ● The window object ● The document object ● The parent object That's why we need to communicate using messages. ©Inbal Geffen 2012