1.sessionStorage
两个页面a.html,b.html
## a.html
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> </head> <body> <script> sessionStorage.setItem('msg','hello world'); console.log(sessionStorage.getItem('msg')); </script> </body> </html>
打印结果 是 : hello world
## b.html <!DOCTYPE html> <html> <head> <meta charset="utf-8"> </head> <body> <script> console.log(sessionStorage.getItem('msg')); </script> </body> </html> 打印结果 是 : null
个人感觉sessionStroage和一个js的变量差不多,作用域是整个页面
2. localStroage
#a.html <!DOCTYPE html> <html> <head> <meta charset="utf-8"> </head> <body> <script> localStorage.setItem('msg','hello world'); console.log(localStorage.getItem('msg')); </script> </body> </html> #结果hello world
# b.html <!DOCTYPE html> <html> <head> <meta charset="utf-8"> </head> <body> <script> console.log(localStorage.getItem('msg')); </script> </body> </html> #结果输出 hello world
#当b.html的不在同一人域名下时输出为null
结论: localStroage和cookie的作用域一样