DEV Community

BekmuhammadDev
BekmuhammadDev

Posted on

Javascript Ls/ss/cookies😎

Browser Memory:

Image description

  • localStorage
  • Session Storage
  • Cookies

Method

SetItem();
GetItem();
RemoveItem();
Clear();

Local Storage
localStorage foydalanuvchi brauzerida ma'lumotlarni uzoq muddat saqlash uchun ishlatiladi. Saqlangan ma'lumotlar brauzer yopilgandan keyin ham saqlanadi.localStorage odatda har bir kelib chiqadigan domen uchun taxminan 5-10 MB xotira ajratiladi. Bu miqdor brauzer va qurilmaga qarab biroz farq qilishi mumkin.

Ma'lumot saqlash(SetItem):

localStorage.setItem('kalit', 'qiymat');

Enter fullscreen mode Exit fullscreen mode

cancole:

Image description

Ma'lumot olish(GetItem):

let qiymat = localStorage.getItem('kalit');
cansole.log(qiymat)
Enter fullscreen mode Exit fullscreen mode

cansole:

Image description

Ma'lumotni o'chirish(removeItem):

localStorage.removeItem('kalit');

Enter fullscreen mode Exit fullscreen mode

Barcha ma'lumotlarni o'chirish(Clear):

localStorage.clear();

Enter fullscreen mode Exit fullscreen mode

Session Storage
sessionStorage ham foydalanuvchi brauzerida ma'lumotlarni saqlash uchun ishlatiladi, lekin bu ma'lumotlar faqat sessiya davomida saqlanadi. Ya'ni, brauzer oynasi yopilganda ma'lumotlar o'chiriladi.sessionStorage ham odatda har bir kelib chiqadigan domen uchun 5-10 MB xotira ajratiladi. Bu miqdor ham brauzer va qurilmaga qarab o'zgarishi mumkin. sessionStorage faqat sessiya davomida ma'lumotlarni saqlaydi va sessiya tugashi bilan (brauzer oynasi yopilganda) ma'lumotlar o'chiriladi.

Ma'lumot saqlash:

sessionStorage.setItem('kalit', 'qiymat');

Enter fullscreen mode Exit fullscreen mode

Ma'lumot olish:

let qiymat = sessionStorage.getItem('kalit');

Enter fullscreen mode Exit fullscreen mode

Ma'lumotni o'chirish:

sessionStorage.removeItem('kalit');

Enter fullscreen mode Exit fullscreen mode

Barcha ma'lumotlarni o'chirish:

sessionStorage.clear();

Enter fullscreen mode Exit fullscreen mode

Cookies
Cookies kichik ma'lumotlar bo'lib, ular brauzerda saqlanadi va veb-saytlar tomonidan o'qilishi mumkin. Cookies maxsus muddat bilan belgilanishi mumkin va brauzer yopilganda yoki maxsus vaqt oralig'ida o'chirilishi mumkin.

Cookie saqlash:

document.cookie = "kalit=qiymat; path=/; max-age=3600"; // 1 soat davomida saqlanadi

Enter fullscreen mode Exit fullscreen mode

Cookie olish:

function getCookie(kalit) {
  let name = kalit + "=";
  let decodedCookie = decodeURIComponent(document.cookie);
  let ca = decodedCookie.split(';');
  for(let i = 0; i < ca.length; i++) {
    let c = ca[i];
    while (c.charAt(0) == ' ') {
      c = c.substring(1);
    }
    if (c.indexOf(name) == 0) {
      return c.substring(name.length, c.length);
    }
  }
  return "";
}

Enter fullscreen mode Exit fullscreen mode

Cookie o'chirish:

document.cookie = "kalit=; path=/; max-age=0";

Enter fullscreen mode Exit fullscreen mode

Image description

Xotira hajmi brauzer va platformaga qarab biroz farq qilishi mumkin. Ba'zi mashhur brauzerlarning localStorage va sessionStorage hajmlari haqida umumiy ma'lumot:

  • Google Chrome: Taxminan 10 MB.
  • Mozilla Firefox: Taxminan 10 MB.
  • Microsoft Edge: Taxminan 10 MB.
  • Safari: Taxminan 5 MB.
  • Opera: Taxminan 10 MB.

Top comments (0)