標籤:
用的jQuery.cookie -----一個封裝好了cookie的外掛程式。(基於jQuery)
我這隻需要調用。
$.cookie("c_name", c_value,{expires:7});
//可通過alert($.cookie("c_name"));得到你要儲存的內容,這樣測試cookie是否儲存;
c_name:要建立的cookie的名字;
c_value:值或者說要儲存的內容;
expires:7儲存日期;
$.cookie("c_name", null);//關閉cookie,可通過alert($.cookie("c_name"))的到null,就可得知已清除cookie。
另外,每隔5分鐘自動儲存一次,即設定定時器
setInterval(function(){
$.cookie("c_name", c_value,{expires:7});
},300000)
$(#input).val($.cookie("c_name"));//讀取cookie,並頁面重新整理時cookie再賦值給原對象
再記錄一下
js擷取iframe裡的元素時(這是在iframe沒有id或者name等的情況下):
var iframe = document.getElementsByTagName(‘iframe‘)[0];
var ifr_document = iframe.contentWindow.document;
$(ifr_document).find(‘#editor‘);//擷取iframe裡id為editor的元素;
jQuery.cookie外掛程式源碼如下:
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();
}
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 {
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]);
if (cookie.substring(0, name.length + 1) == (name + ‘=‘)) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
return cookieValue;
}
};
jQuery.cookie外掛程式用法自我總結