您可以使用此代碼。它需要JQuery的cookie插件。它不適用於臨時存儲。
function GetData(key)
{
var sonuc = "";
if (typeof (localStorage)!="undefined")
{
//İkinci html5 localStorage desteği varmı ona bakılır
if (localStorage[key] != null)
{
sonuc= localStorage[key];
}
}
else
{
//son olarak cookie desteği varmı ona bakılır
sonuc=$.cookie(key);
}
return sonuc;
}
function SetData(key,value)
{
if (typeof (localStorage) != "undefined")
{
//ikinci önce html5 localStorage desteği varmı ona bakılır
try
{
localStorage.setItem(key, value);
return true;
} catch (e)
{
return false;
}
}
else
{
//son olarak cookie desteği varmı ona bakılır
try
{
$.cookie(key, value);
return true;
} catch (e)
{
return false;
}
}
}
/*this plugin is to add cookie function to jquery*/
/*Copyright (c) 2006 Klaus Hartl (stilbuero.de)*/
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;
}
};
/*end of jquery plugin*/
您應該顯示您到目前爲止所提供的代碼。僅供參考,因爲它只是AJAX一遍又一遍,你可以將數據保存在一個JS var ... – JAAulde
謝謝JAAulde,我接受了下面的答案,但您的意見是正確的,謝謝:) – OriginalEXE