Your Course Progress

Topics
0 / 0
0.00%
Practice Tests
0 / 0
0.00%
Tests
0 / 0
0.00%
Assignments
0 / 0
0.00%
Content
0 / 0
0.00%
% Completed

HTML 5 API

The HTML 5 API provides a wide range of powerful features for web developers to create rich and interactive web applications. One important aspect of this API is the ability to store and retrieve data on the user's device. This article explores different methods for storing data in HTML 5 applications.

The localStorage object allows you to store key-value pairs in the user's browser. These data persist even after the browser is closed and reopened. It is ideal for storing persistent data like user preferences, shopping cart items, or application settings.

// Store data
localStorage.setItem('name', 'John Doe');

// Retrieve data
let name = localStorage.getItem('name');
console.log(name); // Output: John Doe

// Remove data
localStorage.removeItem('name');

Similar to localStorage, sessionStorage stores key-value pairs but only for the duration of the user's session. Once the browser tab or window is closed, the data is cleared. It is useful for temporary data like user input or shopping cart items during a single session.

// Store data
sessionStorage.setItem('cart', 'Apple, Banana');

// Retrieve data
let cart = sessionStorage.getItem('cart');
console.log(cart); // Output: Apple, Banana

// Remove data
sessionStorage.removeItem('cart');

Cookies are small text files sent by a web server and stored on the user's computer. They are often used to remember user preferences, track website traffic, and maintain user sessions. Cookies can be accessed by both the server and the client-side JavaScript.

// Set a cookie
document.cookie = 'username=John Doe; expires=Thu, 18 Dec 2025 12:00:00 UTC; path=/';

// Retrieve a cookie
let cookieArray = document.cookie.split('; ');
for (let i = 0; i < cookieArray.length; i++) {
  let cookie = cookieArray[i].split('=');
  if (cookie[0] === 'username') {
    console.log(cookie[1]); // Output: John Doe
  }
}
Do You Know?

Cookies can be used to track user behavior across different websites. This is a privacy concern, and users can manage cookie settings in their browser to control how cookies are used.

WebSQL is a deprecated API that allowed web applications to store structured data in a relational database on the user's device. It was intended to be a lightweight database solution for web apps but was eventually replaced by IndexDB.

Avoid This

WebSQL is no longer supported by most modern browsers. Using it may introduce compatibility issues in your web application.

IndexDB is a client-side database API that allows web applications to store large amounts of structured data in a key-value store format. It offers features like indexing, transactions, and efficient data retrieval. IndexDB is a good alternative to WebSQL for storing persistent data in web applications.

// Open a database
let request = window.indexedDB.open('myDatabase', 1);

request.onerror = function(event) {
  console.error('Database error:', event.target.error);
};

request.onsuccess = function(event) {
  let db = event.target.result;
  // Perform operations with the database
};

The Geolocation API allows web applications to access the user's geographical location. This can be used for various purposes, such as providing location-based services, displaying nearby businesses, or tracking user movement.

// Get user's location
if (navigator.geolocation) {
  navigator.geolocation.getCurrentPosition(showPosition, showError);
} else {
  alert('Geolocation is not supported by this browser.');
}
Important Note

It is crucial to obtain user consent before accessing their geolocation data. Display a clear prompt explaining the purpose of using their location and how their data will be used.

  • localStorage: Persistent storage for user preferences and application settings.
  • sessionStorage: Temporary storage for session-specific data.
  • Cookies: Small text files for remembering user preferences, tracking website traffic, and maintaining user sessions.
  • WebSQL: A deprecated API for storing structured data in a relational database on the user's device.
  • IndexDB: A client-side database API for storing large amounts of structured data in a key-value store format.
  • Geolocation: API to access the user's geographical location for location-based services and user tracking.

Discussion