HTML5 Web Storage

Everything you need to know about HTML5
Site Admin
Posts: 159
Joined: Sun Jun 26, 2022 10:46 pm

HTML5 Web Storage

Post by C0D3D »

Storing data with HTML5 local storage

Example 1

Code: Select all

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Storing Data with HTML5 Local Storage</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script>
// Check if the localStorage object exists
if(localStorage) {
	$(document).ready(function() {
		$(".save").click(function() {
			// Get input name
			var firstName = $("#firstName").val();
			
			// Store data
    		localStorage.setItem("first_name", firstName);
			alert("Your first name is saved.");
		});
		$(".access").click(function() {
			// Retrieve data
    		alert("Hi, " + localStorage.getItem("first_name"));
		});
	});
} else {
    alert("Sorry, your browser do not support local storage.");
}
</script>
</head>
<body>
    <form>
    	<label>First Name: <input type="text" id="firstName"></label>
        <button type="button" class="save">Save Name</button>
        <button type="button" class="access">Get Name</button>
    </form>
</body>
</html>
Storing data with HTML5 session storage

Example 2

Code: Select all

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Storing Data with HTML5 Session Storage</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script>
// Check if the localStorage object exists
if(localStorage) {
	$(document).ready(function() {
		$(".save").click(function() {
			// Get input name
			var lastName = $("#lastName").val();
			
			// Store data
    		sessionStorage.setItem("last_name", lastName);
			alert("Your last name is saved.");
		});
		$(".access").click(function() {
			// Retrieve data
    		alert("Hi, " + localStorage.getItem("first_name") + " " + sessionStorage.getItem("last_name"));
		});
	});
} else {
    alert("Sorry, your browser do not support local storage.");
}
</script>
</head>
<body>
    <form>
    	<label>Last Name: <input type="text" id="lastName"></label>
        <button type="button" class="save">Save Name</button>
        <button type="button" class="access">Get Name</button>
    </form>
</body>
</html>
----------------------------------------------------------------------
Together we are one! One we are many... Much Love from one brother to another...
Working together to achieve amazing things!
Teamwork is key.
Knowledge is Power!

4 Basic datatypes.
Strings: Which store a series of characters.
Int: Which store a full integer or number.
Float: A numeric value with a decimal.
Boolean: Only stores True or False and are very useful with if statements

----------------------------------------------------------------------
ImgBB Image upload

Who is online

Users browsing this forum: No registered users and 0 guests