To save information like name, number, and address in cookies, you can use JavaScript's document.cookie property.
Here's an example of how you could save a user's name and number in a cookie:
typescriptfunction saveCookie(name, number) {
var expires = "expires=Wed, 1 Jan 2023 12:00:00 UTC;";
document.cookie = "name=" + name + ";" + expires + ";path=/";
document.cookie = "number=" + number + ";" + expires + ";path=/";
}
And here's an example of how you could retrieve and display the saved information:
javascriptfunction getCookie(cname) {
var name = cname + "=";
var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie.split(';');
for(var i = 0; i <ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
function displayInfo() {
var name = getCookie("name");
var number = getCookie("number");
document.getElementById("info").innerHTML = "Name: " + name + "<br>Number: " + number;
}
Note that the example above sets the cookie to expire on January 1st, 2023. You can change the expiration date as needed. Additionally, the information stored in cookies is vulnerable to tampering and is not secure. If you need to store sensitive information, it's best to use server-side storage solutions such as a database.

إرسال تعليق