js读åcookieå®ç°ä¸ä¸ªåºé¨å¹¿åæµ®å±ææç两ç§æ¹æ³_javascriptæå·§
程序员文章站
2022-04-28 15:27:46
...
ä¸é¢ä¸ä¸ªæ¡ä¾ä½¿ç¨jså®ç°ä¸ä¸ªé¡µé¢æµ®å±ææï¼å¹¶ä¸éè¿ä¸¤ç§æ¹æ³ä½¿ç¨js读åcookieæ¥å®ç°ç¨æ·å
³é广åçæ¾ç¤ºç¶æï¼
读è å¯ä»¥å°ä¸é¢ä»£ç å¤å¶å°ä¸ä¸ªhtmlæ件è¯è¯ææï¼htmlçpreæ ç¾æªä¸¤ç§jså®ç°çæ¹å¼
IT_Blog_æ¨å¯
读è å¯ä»¥å°ä¸é¢ä»£ç å¤å¶å°ä¸ä¸ªhtmlæ件è¯è¯ææï¼htmlçpreæ ç¾æªä¸¤ç§jså®ç°çæ¹å¼
å¤å¶ä»£ç 代ç å¦ä¸:
1.第ä¸ç§ï¼ä½¿ç¨jQueryçcookieåº
ä¾åå°±æ¯ç°å¨æ£å¨ä½¿ç¨çjs,ç¸å ³ä»£ç å¦ä¸ï¼
$(document).ready(function () {
var adCookie=$.cookie("docCookie");
//å¦ææ¬å°æ²¡æcookieï¼å°è¯æ¡cookieåå ¥æ¬å°
if(adCookie!="adDocCookie"){
$("#wapDocCookie").show();
}
//å¦ææ¬å°åå¨è¯æ¡cookieï¼ä¸æ¾ç¤ºæµ®å±
if(adCookie=="adDocCookie"){
$("#wapDocCookie").hide();
}
//å ³é广åï¼éèæµ®å±
$("#closeAd").click(function(){
$("#wapDocCookie").hide();
$.cookie("docCookie","adDocCookie",{expires:60});
});
});
//jQuery cookie library
jQuery.cookie = function(name, value, options) {
if (typeof value != 'undefined') { // name and value given, set cookie
options = options || {};
if (value === null) {
value = '';
options.expires = -1;
}
var expires = '';
if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
var date;
if (typeof options.expires == 'number') {
date = new Date();
date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
} else {
date = options.expires;
}
expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
}
var path = options.path ? '; path=' + (options.path) : '';
var domain = options.domain ? '; domain=' + (options.domain) : '';
var secure = options.secure ? '; secure' : '';
document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
} else { // only name given, get cookie
var cookieValue = null;
if (document.cookie && document.cookie != '') {
var cookies = document.cookie.split(';');
for (var i = 0; i var cookie = jQuery.trim(cookies[i]);
// Does this cookie string begin with the name we want?
if (cookie.substring(0, name.length + 1) == (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
return cookieValue;
}
};
2.第äºç§ï¼èªå·±åä¸ä¸ªjsæä½cookieçå®ä¾
ç¸å ³jså¦ä¸ï¼
$(document).ready(function() {
function writeCookie(name,value)
{
var exp = new Date();
exp.setTime(exp.getTime() + 7*24*60*60*1000);
document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
}
//读åcookies
function readCookie(name)
{
var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
if(arr=document.cookie.match(reg)){
return unescape(arr[2]);
}else {
return null;
}
}
var adCookie = readCookie("adCookie");
if(adCookie!="adDocCookie"){
$("#wapDocCookie").show();
}
//å¦ææ¬å°åå¨è¯æ¡cookieï¼ä¸æ¾ç¤ºæµ®å±
if(adCookie=="adDocCookie"){
$("#wapDocCookie").hide();
}
//å ³é广åï¼éèæµ®å±
$("#closeAd").click(function(){
$("#wapDocCookie").hide();
});
});
上一篇: 一份PHP的面试题_PHP教程
下一篇: 如何使用ps修改图片尺寸