Web Storage API


فهرست مطالب

    نمایش فهرست مطالب


Web Storage API ﺖﺳﺍ ﺮﮔﺭﻭﺮﻣ ﺭﺩ ﺎﻫ ﻩﺩﺍﺩ ﯽﺑﺎﯾﺯﺎﺑ ﻭ ﻩﺮﯿﺧﺫ ﯼﺍﺮﺑ ﻩﺩﺎﺳ ﻮﺤﻧ ﮏﯾ.

ﻝﺎﺜﻣ

localStorage.setItem("name", "John Doe");
localStorage.getItem("name");

→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ

<!DOCTYPE html>
<html>
<body>

<p id="demo"></p>

<script>
localStorage.setItem("name","John Doe");
document.getElementById("demo").innerHTML = localStorage.getItem("name");
</script>

</body>
</html>

Web Storage API ﺩﻮﺷ ﯽﻣ ﯽﻧﺎﺒﯿﺘﺸﭘ ﺎﻫﺮﮔﺭﻭﺮﻣ ﻪﻤﻫ ﺭﺩ:

Chrome IE/Edge Firefox Safari Opera
Yes Yes Yes Yes Yes

LocalStorage Object

.ﺪﻨﮐ ﯽﻣ ﻢﻫﺍﺮﻓ ﺹﺎﺧ ﺖﯾﺎﺳ ﺏﻭ ﮏﯾ ﯼﺍﺮﺑ ﺍﺭ ﯽﻠﺤﻣ ﯼﺯﺎﺳ ﻩﺮﯿﺧﺫ ﯼﺎﻀﻓ ﮏﯾ ﻪﺑ ﯽﺳﺮﺘﺳﺩ localStorage ﯽ

.ﺪﻧﻮﺷ ﯽﻤﻧ ﻑﺬﺣ ﺮﮔﺭﻭﺮﻣ ﻥﺪﺷ ﻪﺘﺴﺑ ﺎﺑ ﻭ ﺪﻧﻮﺷ ﯽﻣ ﻩﺮﯿﺧﺫ ﺎﻀﻘﻧﺍ ﺦﯾﺭﺎﺗ ﻥﻭﺪﺑ ﺎﻫ ﻩﺩﺍﺩ

.ﺩﻮﺑ ﺪﻨﻫﺍﻮﺧ ﺱﺮﺘﺳﺩ ﺭﺩ ﺎﻫ ﻝﺎﺳ ﻭ ﺎﻫ ﻪﺘﻔﻫ ،ﺎﻫﺯﻭﺭ ﯼﺍﺮﺑ ﺎﻫ ﻩﺩﺍﺩ


.()setItem ﺪﺘﻣ

.ﺪﻨﮐ ﯽﻣ ﻩﺮﯿﺧﺫ ﻪﻈﻓﺎﺣ ﮏﯾ ﺭﺩ ﺍﺭ ﻩﺩﺍﺩ ﻢﺘﯾﺁ ﮏﯾ()localStorage.setItem ﺪﺘﻣ

:ﺩﺮﯿﮔ ﯽﻣ ﺮﺘﻣﺍﺭﺎﭘ ﻥﺍﻮﻨﻋ ﻪﺑ ﺭﺍﺪﻘﻣ ﮏﯾ ﻭ ﻡﺎﻧ ﮏﯾ

ﻝﺎﺜﻣ

localStorage.setItem("name", "John Doe");

.()getItem ﺪﺘﻣ

.ﺪﻨﮐ ﯽﻣ ﯽﺑﺎﯾﺯﺎﺑ ﻪﻈﻓﺎﺣ ﺯﺍ ﺍﺭ ﻩﺩﺍﺩ ﻢﺘﯾﺁ ﮏﯾ()localStorage.getItem ﺪﺘﻣ

:ﺩﺮﯿﮔ ﯽﻣ ﺮﺘﻣﺍﺭﺎﭘ ﻥﺍﻮﻨﻋ ﻪﺑ ﻡﺎﻧ ﮏﯾ

ﻝﺎﺜﻣ

localStorage.getItem("name");


sessionStorage ﯽﺷ

.ﺖﺳﺍ ﻥﺎﺴﮑﯾ localStorage ﯽﺷ ﺎﺑ sessionStorage ﯽﺷ

.ﺪﻨﮐ ﯽﻣ ﻩﺮﯿﺧﺫ ﻪﺴﻠﺟ ﮏﯾ ﯼﺍﺮﺑ ﺍﺭ ﺎﻫ ﻩﺩﺍﺩ sessionStorage ﯽﺷ ﻪﮐ ﺖﺳﺍ ﻦﯾﺍ ﺕﻭﺎﻔﺗ

.ﺪﻧﻮﺷ ﯽﻣ ﻑﺬﺣ ﺎﻫ ﻩﺩﺍﺩ ﺮﮔﺭﻭﺮﻣ ﻥﺪﺷ ﻪﺘﺴﺑ ﺎﺑ

ﻝﺎﺜﻣ

sessionStorage.getItem("name");

→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ

<!DOCTYPE html>
<html>
<body>

<p id="demo"></p>

<script>
sessionStorage.setItem("name","John Doe");
document.getElementById("demo").innerHTML = sessionStorage.getItem("name");
</script>

</body>
</html>

.()setItem ﺪﺘﻣ

.ﺪﻨﮐ ﯽﻣ ﻩﺮﯿﺧﺫ ﻪﻈﻓﺎﺣ ﮏﯾ ﺭﺩ ﺍﺭ ﻩﺩﺍﺩ ﻢﺘﯾﺁ ﮏﯾ()sessionStorage.setItem ﺪﺘﻣ

:ﺩﺮﯿﮔ ﯽﻣ ﺮﺘﻣﺍﺭﺎﭘ ﻥﺍﻮﻨﻋ ﻪﺑ ﺭﺍﺪﻘﻣ ﮏﯾ ﻭ ﻡﺎﻧ ﮏﯾ

ﻝﺎﺜﻣ

sessionStorage.setItem("name", "John Doe");

.()getItem ﺪﺘﻣ

.ﺪﻨﮐ ﯽﻣ ﯽﺑﺎﯾﺯﺎﺑ ﻪﻈﻓﺎﺣ ﺯﺍ ﺍﺭ ﻩﺩﺍﺩ ﻢﺘﯾﺁ ﮏﯾ()sessionStorage.getItem ﺪﺘﻣ

:ﺩﺮﯿﮔ ﯽﻣ ﺮﺘﻣﺍﺭﺎﭘ ﻥﺍﻮﻨﻋ ﻪﺑ ﻡﺎﻧ ﮏﯾ

ﻝﺎﺜﻣ

sessionStorage.getItem("name");

ﯼﺯﺎﺳﻩﺮﯿﺧﺫ ﯽﺷ ﯼﺎﻫﺵﻭﺭ ﻭ ﺎﻫﯽﮔﮋﯾﻭ

key(n)

ﺪﻧﺍﺩﺮﮔﯽﻣﺮﺑ ﺍﺭ ﻪﻈﻓﺎﺣ ﺭﺩ ﻡﺍn ﺪﯿﻠﮐ ﻡﺎﻧ

length

ﺪﻧﺍﺩﺮﮔ ﯽﻣﺮﺑ ﺍﺭ Storage ﯽﺷ ﺭﺩ ﻩﺪﺷ ﻩﺮﯿﺧﺫ ﻩﺩﺍﺩ ﻡﻼﻗﺍ ﺩﺍﺪﻌﺗ

getItem(keyname)

ﺪﻧﺍﺩﺮﮔ ﯽﻣﺮﺑ ﺍﺭ ﻩﺪﺷ ﺺﺨﺸﻣ ﺪﯿﻠﮐ ﻡﺎﻧ ﺭﺍﺪﻘﻣ

setItem(keyname, value)

(ﺪﺷﺎﺑ ﻪﺘﺷﺍﺩ ﺩﻮﺟﻭ ﻞﺒﻗ ﺯﺍ ﺮﮔﺍ) ﺪﻨﮐ ﯽﻣ ﺯﻭﺭ ﻪﺑ ﺍﺭ ﺪﯿﻠﮐ ﺭﺍﺪﻘﻣ ﮏﯾ ﺎﯾ ﺪﻨﮐ ﯽﻣ ﻪﻓﺎﺿﺍ ﯼﺯﺎﺳ ﻩﺮﯿﺧ

removeItem(keyname)

ﺪﻨﮐ ﯽﻣ ﺝﺭﺎﺧ ﻪﻈﻓﺎﺣ ﺯﺍ ﺍﺭ ﺪﯿﻠﮐ ﻥﺁ

clear()

ﺪﯿﻨﮐ ﯽﻟﺎﺧ ﯼﺯﺎﺳ ﻩﺮﯿﺧﺫ ﯼﺎﻀﻓ ﺯﺍ ﺍﺭ ﺪﯿﻠﮐ ﻡﺎﻤﺗ

Web Storage API ﯼﺍﺮﺑ ﻂﺒﺗﺮﻣ ﺕﺎﺤﻔﺻ

window.localStorage

ﺪﻨﮐ ﯽﻣ ﻩﺮﯿﺧﺫ ﻩﺭﺎﻤﺷ ﺎﺑ ﺍﺭ ﺎﻫ ﻩﺩﺍﺩ .ﺪﻨﮐ ﯽﻣ ﻢﻫﺍﺮﻓ ﺍﺭ ﺏﻭ ﺮﮔﺭﻭﺮﻣ ﺭﺩ ﺭﺍﺪﻘﻣ/ﺪﯿﻠﮐ ﯼﺎﻫ ﺖﻔﺟ ﻩﺮﯿ

window.sessionStorage

ﺪﻨﮐ ﯽﻣ ﻩﺮﯿﺧﺫ ﮏﯾ ﯼﺍﺮﺑ ﺍﺭ ﺎﻫ ﻩﺩﺍﺩ .ﺪﻨﮐ ﯽﻣ ﻢﻫﺍﺮﻓ ﺍﺭ ﺏﻭ ﺮﮔﺭﻭﺮﻣ ﺭﺩ ﺭﺍﺪﻘﻣ/ﺪﯿﻠﮐ ﯼﺎﻫ ﺖﻔﺟ ﻩﺮﯿﺧ