2011-02-03 81 views
1

嗨,我有這樣的僞代碼:整合jQuery的餅乾

if(cookie set){ 
    $("#notification").hide(); 
} 
else { 
    $("#notification").show(); 
} 

如何集成了jQuery插件的Cookie,因此如果它發現一個Cookie,則它不顯示#notification?

感謝

回答

2
if ($.cookie('whatever')) { 
    //exists 
} else { 
    //nuthin! 
} 

提供你正在使用:http://plugins.jquery.com/project/Cookie

[編輯]你還可以:

if ($.cookie('whatever') !== null) { 
    //exists 
} else { 
    //still nuthin 
} 
2

假設你使用this插件:

if ($.cookie('the_cookie')) DO_YOUR_WORK; 
1

此代碼爲我工作:

<script> 
    if ($.cookie('whatever')) { 
     alert('exists'); 
    } else { 
     alert('not exists'); 
    } 
    //$.cookie('whatever', 'asd'); writing cookie.. 
</script> 
2

Cookies是最重要的事情或技術存儲在客戶端環境數據。 jQuery Cookie是一個簡單輕量的插件,用於從客戶端寫入,讀取和刪除cookie。

首先,您需要下載jquery.cookie插件,只需將其添加到您的頁面即可。

<head> 
<script src="js/jquery.js"></script> 
<script src="js/jquery.cookie.js"></script> 
</head> 

設置cookie的:

$.cookie('cookie_name', 'cookie_value', { expires: 7 }); 

獲取的Cookie:

$.cookie('cookie_name'); 

刪除Cookie:

$.cookie('cookie_name', null, { path: '/' }); 

閱讀:jQuery Cookie (Set, Get and Delete)