2010-12-17 77 views
4

我已經通過論壇並閱讀了許多與我的問題有關的帖子,但我找不到我正在尋找的答案。在頁面加載時顯示啓用Cookie的Jquery通知消息

我想在頁面加載時向用戶顯示帶有jquery的消息,並允許他們通過單擊關閉鏈接來關閉div。一旦關閉,腳本應該記住在該會話期間不要再打開。我想它會使用一個cookie。

這裏的XHTML:

<div class="alertbox"> 
    <span class="message">Please upgrade your browser</span> 
    <div class="bloc_navigateurs"> 
    <a href="http://download.mozilla.org/?product=firefox-3.6.13&amp;os=win&amp;lang=fr" target="_blank"><img src="includes/themes/default/images/icons/misc/Firefox-16.gif" width="16" height="16" alt="Firefox" /></a> 
    <a href="http://www.google.com/chrome?hl=fr" target="_blank"><img src="includes/themes/default/images/icons/misc/Chrome-16.gif" width="16" height="16" alt="Chrome" /></a> 
    <a href="http://www.01net.com/telecharger/windows/Internet/navigateur/fiches/13759.html" target="_blank"><img src="includes/themes/default/images/icons/misc/IE-16.gif" width="16" height="16" alt="Internet Explorer " /></a> 
    </div> 
    <span class="close">close</span> 
</div> 

正如你所看到的,這個建議的IE6用戶升級他們的瀏覽器

+0

哎呀,非常有意義的消息:)我喜歡那樣。 – Fatih 2010-12-17 22:45:13

回答

3

我會使用jQuery cookie plugin

爲創建cookie的關閉鏈接的onclick事件綁定處理程序。在顯示div之前,請檢查cookie。

<script> 
$(function() { 
    var $box = $(".alertbox"), 
     stateCookieName = 'alertbox_state', 
     alreadyClosed = $.cookie(stateCookieName); 

    // The box should already be hidden initially (using CSS preferrably). 
    // If not, uncomment the line below: 
    // $box.hide(); 

    // Show the box if it hasn't already been closed. 
    if (alreadyClosed != 1) { 
    $box.show(); 
    } 

    $box.find('.close').click(function() { 
    $box.hide(); 
    $.cookie(stateCookieName, 1); 
    }); 
}); 
</script>
+0

嗨simshaun,非常感謝你這麼快的回覆。我只是測試了建議的代碼,並沒有奏效。該div仍然被CSS關閉。我正在使用Chrome 8 – user546585 2010-12-17 22:57:55

+0

你能發佈一個URL,以便我可以測試嗎?它已經很晚了,我的大腦可能會被炸,但是我發佈的內容對我來說看起來是正確的。 :) – simshaun 2010-12-17 22:59:19

+0

嘿Simshaun,這是一個本地項目沒有在網絡上託管。我是否還需要修改代碼,並添加 user546585 2010-12-17 23:25:53

0

得到here cookie的功能,那麼你可以設置alertBox被默認爲隱藏和:

$(document).ready(function(){ 
    if(readCookie('warnedIE')==null){ 
    $('.alertbox').show(); 
    createCookie('warnedIE','true',30); 
    } 
});