代码如下:
function getCookie(key){ var cookies=document.cookie; if(cookies.length>0){ var start=cookies.indexOf(key+"="); if(start<0){ return ""; } var end =cookies.indexOf(";",start); if(end<0){ end=cookies.length; } return cookies.substring(start+key.length+1,end); } }
或者
function getCookie(cookieName) { var strCookie = document.cookie; var arrCookie = strCookie.split("; "); for(var i = 0; i < arrCookie.length; i++){ var arr = arrCookie[i].split("="); if(cookieName == arr[0]){ return arr[1]; } } return ""; }
简单来说浏览器的cookie是通过document.cookie存放的格式类似于,"t=2;r=3;t=4"的形式,按照这个形式进行截取就好了