Jquery和js實現cookie操作手機浮層廣告;附加:js獲取、新增、刪除cookie
1、jquery cookie包實現手機上的浮層廣告
<span style="font-size:18px;">$(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 < cookies.length; 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;
}
};</span>
2.js cookie實現手機上的浮層廣告
<span style="font-size:18px;">$(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 docCookie = readCookie("docCookie");
var docFootAds = $("#guancha_foot_ads");
if(docCookie!="docWirteCookie"){
alert(docCookie);
writeCookie("docCookie","docWirteCookie");
docFootAds.show();
}
if(docCookie=="docWirteCookie"){
alert(docCookie);
docFootAds.hide();
}
//關閉廣告,隱藏浮層
$("#closeAd").click(function(){
$("#wapDocCookie").hide();
});
});</span>
3..網上js操作cookie的資料
js獲取cookie
<span style="font-size:18px;">//js獲取cookie
/*獲取Cookie值*/
function getCookie(c_name)
{
if(document.cookie.length>0){
c_start=document.cookie.indexOf(c_name + "=")
if(c_start!=-1){
c_start=c_start + c_name.length+1
c_end=document.cookie.indexOf(";",c_start)
if(c_end==-1) c_end=document.cookie.length
return unescape(document.cookie.substring(c_start,c_end))
}
}
return ""
}</span>
**********************************//JS操作cookies方法! *****************************************************************
<span style="font-size:18px;">//寫cookies
function setCookie(name,value)
{
var Days = 30;
var exp = new Date();
exp.setTime(exp.getTime() + Days*24*60*60*1000);
document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
}
//讀取cookies
function getCookie(name)
{
var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
if(arr=document.cookie.match(reg))
return unescape(arr[2]);
else
return null;
}
//刪除cookies
function delCookie(name)
{
var exp = new Date();
exp.setTime(exp.getTime() - 1);
var cval=getCookie(name);
if(cval!=null)
document.cookie= name + "="+cval+";expires="+exp.toGMTString();
}
//使用示例
setCookie("name","hayden");
alert(getCookie("name"));
//如果需要設定自定義過期時間
//那麼把上面的setCookie 函式換成下面兩個函式就ok;
//程式程式碼
function setCookie(name,value,time)
{
var strsec = getsec(time);
var exp = new Date();
exp.setTime(exp.getTime() + strsec*1);
document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
}
function getsec(str)
{
alert(str);
var str1=str.substring(1,str.length)*1;
var str2=str.substring(0,1);
if (str2=="s")
{
return str1*1000;
}
else if (str2=="h")
{
return str1*60*60*1000;
}
else if (str2=="d")
{
return str1*24*60*60*1000;
}
}
//這是有設定過期時間的使用示例:
//s20是代表20秒
//h是指小時,如12小時則是:h12
//d是天數,30天則:d30
setCookie("name","hayden","s20");</span>
相關文章
- Cookie新增、獲取以及刪除操作Cookie
- 新增cookie、刪除cookie、清除cookieCookie
- Python Flask,cookie,設定、獲取、刪除cookiePythonFlaskCookie
- cookie的設定、獲取和刪除Cookie
- cookie外掛-jquery.cookie.js介紹CookiejQueryJS
- 如何設定cookie和刪除cookieCookie
- JavaScript 刪除cookieJavaScriptCookie
- js-cookie.js的使用JSCookie
- 如何獲取Cookie並使用Cookie侵入Cookie
- HttpServletRequest 獲取 CookieHTTPServletCookie
- gatling 獲取 cookieCookie
- jQuery 新增和刪除classjQuery
- 使用requests獲取並向headers中新增cookieHeaderCookie
- Python Cookie HTTP獲取cookie並處理PythonCookieHTTP
- request 獲取不到 CookieCookie
- 獲取cookie辦法Cookie
- js清除所有的cookieJSCookie
- Vue+Element+JS動態生成form表單實現新增和刪除操作VueJSORM
- JS筆記-007-JS Window-cookieJS筆記Cookie
- requests模組獲取cookieCookie
- 獲取cookie裡面的值Cookie
- java後臺獲取cookieJavaCookie
- Java操作Cookie之新增CookieJavaCookie
- jQuery為元素新增和刪除classjQuery
- 新增cookieCookie
- Python教程分享:Python Cookie HTTP獲取cookie並處理PythonCookieHTTP
- 通過jquery.cookie.js實現記住使用者名稱、密碼登入功能jQueryCookieJS密碼
- 利用mitmproxy實現抖音Cookie,裝置ID獲取(一)MITCookie
- 利用 mitmproxy 實現抖音 Cookie,裝置 ID 獲取 (一)MITCookie
- 我理解的 Cookie 讀/寫/刪除Cookie
- jQuery動態新增和刪除表格行jQuery
- jQuery table表格行的新增和刪除jQuery
- Cookie 作用,互動過程解析,設定,獲取,刪除,生效時間的設定Cookie
- js實現獲取URL引數JS
- jQuery的Cookie使用jQueryCookie
- JS,Jquery獲取各種螢幕的寬度和高度JSjQuery
- jQuery動態新增和刪除表格記錄jQuery
- js cookie 頁面倒數計時JSCookie
- JS 基礎篇(十) cookie封裝JSCookie封裝