Có an toàn khi sử dụng bộ nhớ cục bộ trong javascript không?

Bộ nhớ cục bộ cho phép nhà phát triển lưu trữ và truy xuất dữ liệu trong trình duyệt. Dữ liệu được lưu trữ trong bộ nhớ cục bộ sẽ không hết hạn. Điều này có nghĩa là dữ liệu sẽ tồn tại ngay cả khi tab hoặc cửa sổ trình duyệt bị đóng

điều kiện tiên quyết

Bạn phải có hiểu biết cơ bản về JavaScript. Bạn cũng cần một trình chỉnh sửa mã và trình duyệt để kiểm tra dự án. Trong hướng dẫn này, chúng tôi sẽ sử dụng Visual Studio Code và Google Chrome

Lưu trữ cục bộ là gì

Lưu trữ cục bộ là một dạng lưu trữ web lưu trữ dữ liệu trong một thời gian dài. Đây có thể là một ngày, một tuần hoặc thậm chí một năm. Điều này phụ thuộc vào sở thích của nhà phát triển. Điều quan trọng cần lưu ý là bộ nhớ cục bộ chỉ lưu trữ các chuỗi, vì vậy, nếu bạn muốn lưu trữ các đối tượng, danh sách hoặc mảng, bạn phải chuyển đổi chúng thành chuỗi bằng cách sử dụng

window.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.
6

Khi nào nên sử dụng bộ nhớ cục bộ

Bạn chỉ nên sử dụng bộ nhớ cục bộ khi lưu trữ thông tin nhạy cảm. Điều này là do các cá nhân bên thứ ba có thể dễ dàng truy cập thông tin. Bộ nhớ cục bộ có thể giúp lưu trữ dữ liệu tạm thời trước khi được đẩy lên máy chủ. Điều quan trọng là phải xóa bộ nhớ cục bộ sau khi thao tác này hoàn tất

Hạn chế

Những hạn chế chính của lưu trữ cục bộ là

  • dữ liệu không an toàn

  • Hoạt động đồng bộ

  • Dung lượng lưu trữ hạn chế

Các phương thức chính trong lưu trữ cục bộ

Các phương thức chính khi sử dụng bộ nhớ cục bộ là ________ 07, ________ 08, ________ 09, _______ 40 và

const Car = {
  brand:"Suzuki",
  color:"white",
  price:10000
}

window.localStorage.setItem['car', JSON.stringify[Car]];
1

Chìa khóa[]

Phương thức này được sử dụng để truy xuất một giá trị/chuỗi từ một vị trí cụ thể. Chỉ mục có thể được chuyển vào hàm

window.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.
7 dưới dạng tham số

var answer = localStorage.key[1];
// this statement will retrieve the value of the second item in localStorage.

window.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.
7 cũng có thể được sử dụng trong câu lệnh lặp để truy xuất tất cả các mục trong bộ nhớ cục bộ

thiết lập các mục[]

Chức năng này được sử dụng để lưu trữ các mục trong bộ nhớ cục bộ. Một ví dụ về chức năng này được hiển thị bên dưới

window.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.

Như đã đề cập trước đó, chúng ta phải

const Car = {
  brand:"Suzuki",
  color:"white",
  price:10000
}

window.localStorage.setItem['car', JSON.stringify[Car]];
4 đối tượng trước khi lưu trữ chúng trong bộ lưu trữ cục bộ

Một ví dụ được phác thảo dưới đây

const Car = {
  brand:"Suzuki",
  color:"white",
  price:10000
}

window.localStorage.setItem['car', JSON.stringify[Car]];

Không xâu chuỗi đối tượng sẽ dẫn đến lỗi

getItem[]

Chức năng này được sử dụng để truy cập hoặc lấy dữ liệu trong bộ nhớ cục bộ. Phương thức này lấy một

const Car = {
  brand:"Suzuki",
  color:"white",
  price:10000
}

window.localStorage.setItem['car', JSON.stringify[Car]];
5 làm tham số. Sau đó, nó trích xuất giá trị cần thiết từ localStorage

Ví dụ, để lấy đối tượng

const Car = {
  brand:"Suzuki",
  color:"white",
  price:10000
}

window.localStorage.setItem['car', JSON.stringify[Car]];
6 ở trên, chúng ta sẽ sử dụng câu lệnh sau

window.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.
4

Câu lệnh trên sẽ trả về một cái gì đó như thế này

window.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.
5

Bạn nên chuyển đổi nó thành một đối tượng bằng cách sử dụng

const Car = {
  brand:"Suzuki",
  color:"white",
  price:10000
}

window.localStorage.setItem['car', JSON.stringify[Car]];
7 để sử dụng nó trong mã của bạn

window.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.
7

loại bỏ mục[]

Phương pháp này được sử dụng để xóa một mục khỏi bộ nhớ cục bộ. Phương thức

window.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.
9 yêu cầu khóa làm tham số

window.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.
9

xa lạ[]

Phương pháp này được sử dụng để xóa tất cả các giá trị được lưu trữ trong bộ nhớ cục bộ. Nó không yêu cầu bất kỳ tham số nào

window.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.
0

Dự án

Bây giờ chúng ta đã tìm hiểu về các chức năng chính của bộ nhớ cục bộ, hãy tạo một ứng dụng web lưu trữ, truy xuất, xóa và xóa các mục khỏi bộ nhớ cục bộ

Tạo một thư mục mới và mở nó trong trình chỉnh sửa mã của bạn. Tạo hai tệp,

const Car = {
  brand:"Suzuki",
  color:"white",
  price:10000
}

window.localStorage.setItem['car', JSON.stringify[Car]];
9 và
window.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.
40. Tệp
const Car = {
  brand:"Suzuki",
  color:"white",
  price:10000
}

window.localStorage.setItem['car', JSON.stringify[Car]];
9 sẽ hiển thị trang web cho người dùng, trong khi tệp
window.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.
40 sẽ lưu trữ các chức năng JavaScript của chúng tôi. Các chức năng này sẽ được sử dụng để truy cập các chức năng khác nhau của bộ nhớ cục bộ

Hãy viết mã

const Car = {
  brand:"Suzuki",
  color:"white",
  price:10000
}

window.localStorage.setItem['car', JSON.stringify[Car]];
9 của chúng ta sẽ có một
window.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.
44 và một số
window.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.
45, như hình bên dưới

window.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.
8

Khi nhấp vào nút

window.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.
46, nó sẽ nhận đầu vào của người dùng và chuyển nó đến hàm
window.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.
47 trong tệp
window.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.
40.
window.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.
49 nhận đầu vào của người dùng. Các giá trị này sau đó được chuyển đến đối tượng xe hơi và được lưu trữ trong bộ nhớ cục bộ bằng phương thức
window.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.
50

window.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.
4

Tương tự,

window.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.
51 sẽ gọi hàm
window.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.
52 khi được nhấp vào. Phương thức này tìm nạp các mục từ localStorage bằng hàm getItem

window.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.
53 tạo một thành phần đoạn văn mới trong trang web của chúng tôi

window.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.
54 giúp tạo văn bản sẽ hiển thị cho người dùng

Nút văn bản sau đó được thêm vào thẻ đoạn bởi

window.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.
55

Các thành phần này sau đó được hiển thị ở một vị trí cụ thể trên trang web bởi

window.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.
56 và
window.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.
57

window.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.
0

window.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.
58 gọi
window.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.
9.
window.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.
70 này sẽ xóa một giá trị khỏi bộ nhớ cục bộ bằng hàm
window.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.
71

window.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.
1

window.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.
72 gọi
window.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.
73. Phương pháp
const Car = {
  brand:"Suzuki",
  color:"white",
  price:10000
}

window.localStorage.setItem['car', JSON.stringify[Car]];
1 được sử dụng để xóa tất cả các giá trị trong bộ nhớ cục bộ

window.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.
2

Hãy đặt thuộc tính

window.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.
75 của tất cả các nút khi tải trang web

window.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.
3

Đây là tệp

window.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.
40 với tất cả các chức năng

window.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.
4

Như đã trình bày ở trên, các chức năng sẽ chỉ có thể truy cập được sau khi tải xong trang. Điều này được chỉ định bởi phương pháp

window.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.
77

Đảm bảo rằng tệp

window.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.
40 được tham chiếu trong tệp
const Car = {
  brand:"Suzuki",
  color:"white",
  price:10000
}

window.localStorage.setItem['car', JSON.stringify[Car]];
9 bằng cách dán câu lệnh bên dưới vào phần
window.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.
90

window.localStorage.setItem["grade","One"];
//in this case, the `grade` is the key while `One` is the value.
5

Kết quả

Video sau đây cho thấy cách trang web hoạt động

Sự kết luận

Bây giờ bạn đã quen thuộc với các chức năng khác nhau của bộ nhớ cục bộ. Các phương thức chính trong lưu trữ cục bộ là ________ 150, ________ 192, ________ 171 và ________ 194. Cần có

const Car = {
  brand:"Suzuki",
  color:"white",
  price:10000
}

window.localStorage.setItem['car', JSON.stringify[Car]];
5 khi lưu trữ, truy xuất và xóa các mục khỏi bộ lưu trữ cục bộ. Trong trường hợp bạn không hiểu bất kỳ khái niệm nào, vui lòng xem lại các chức năng lưu trữ cục bộ

LocalStorage có an toàn trong JavaScript không?

Mặt khác, localStorage có thể dễ bị tấn công bởi các cuộc tấn công cross-site scripting [XSS] . Nếu kẻ tấn công có thể đưa JavaScript độc hại vào trang web, thì chúng có thể đánh cắp mã thông báo truy cập trong localStorage. Ngoài ra, không giống như cookie, localStorage không cung cấp các thuộc tính bảo mật mà bạn có thể đặt để chặn các cuộc tấn công.

Sử dụng bộ nhớ cục bộ có tốt không?

Bộ nhớ cục bộ cung cấp ít nhất 5 MB dung lượng lưu trữ dữ liệu trên tất cả các trình duyệt web chính, lớn hơn rất nhiều so với 4KB [kích thước tối đa] mà bạn có thể lưu trữ trong cookie. Điều này làm cho bộ nhớ cục bộ đặc biệt hữu ích nếu bạn muốn lưu vào bộ nhớ cache một số dữ liệu ứng dụng trong trình duyệt để sử dụng sau này .

Những rủi ro của lưu trữ cục bộ là gì?

Các giới hạn và cân nhắc khi sử dụng bộ nhớ cục bộ. .
Nó không an toàn, có thể được truy cập bằng các công cụ dành cho nhà phát triển trình duyệt, vì vậy đừng sử dụng nó để lưu trữ dữ liệu nhạy cảm
Người dùng có thể xóa nó khi xóa tất cả lịch sử trình duyệt
Nó chỉ có thể lưu trữ dữ liệu chuỗi
Nó là đồng bộ.
Nó không thể được sử dụng bởi nhân viên web

Bộ nhớ cục bộ có an toàn hơn cookie không?

LocalStorage/SessionStorage dễ bị tấn công XSS. Mã thông báo truy cập có thể được đọc bằng JavaScript. Cookie, với các cờ httpOnly, secure và SameSite=strict, sẽ an toàn hơn

Chủ Đề