关于前端页面之间传递参数的获取方法和存储数据(缓存)
1.参数获取
//获取参数的方法
function GetQueryString(name){
var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)",“i”);
var r = window.location.search.substr(1).match(reg);
if(r!=null)return unescape(r[2]);
return null;
}
2.存储数据(缓存)
关于前端存储的方法用很多,最为常见的是cookie和locationStorage。
其中cookie的限制比较大,比如必须需要cookie.js、浏览器对cookie的数量限制(之前的浏览器每个域名限制cookie之恶能存储50个),数据长度的限制(大部分浏览器是4085字节左右,但实际更小)。
locationstorage能存储4-5M大小的数据,有效期未永久。
application cache 是离线缓存,存储大小也是5M左右,他与其他两种方式的不同在与浏览器缓存是对单个文件,而applocation cache是对整个应用,当整个应用都缓存下来的时候可以达到无网络连接。
今天我要写的是locationstorage的sessionStorage,他们的用法完全相同,很简单,但是真的很实用。今天使用之后我觉得这个sessionStorage真的是太方便了。
sessionStorage
//存储
sessionStorage.setItem('newList',str)
//获取
sessionStorage.getItem('newList')