2013-08-01 24 views
0

我創建了一個簡單的div元素與切換元件jQuery的關閉格窗口+餅乾

jQuery('.float_close').click(function() { 
       jQuery('.float_notice').fadeToggle('slow'); 

modal

是多麼困難,以保持它通過使用可能關閉通知,關閉按鈕餅乾?

回答

2

不是很難,但我建議在jQuery之上使用額外的JavaScript插件(http://plugins.jquery.com/cookie/)。

if ($.cookie('noticeVisibility') !== undefined 
     && $.cookie('noticeVisibility') === 'hidden') { 

    // If a cookie that stores the visibility 
    // of the notice was set and it says 'hidden', hide the notice 
    jQuery('.float_notice').hide(); 
} 

jQuery('.float_close').click(function() { 
    jQuery('.float_notice').fadeToggle('slow'); 

    // Saves a cookie for later 
    $.cookie('noticeVisibility', 'hidden'); 
}); 
1

你想要的模式不加載再次曾經(或某些特定的時間框架內),即使用戶再次訪問該頁面?如果這就是你所要做的,那麼一個餅乾可以被接受,但不是理想的;如果用戶禁用了他們的cookies,他們將連續出現模式

如果你想使用cookie,你只需設置它的單擊事件的內部,並檢查其是否觸發模式(或結合到最終觸發模式時)之前被設置。設置cookie非常簡單(有關教程和某些功能,請參閱http://www.quirksmode.org/js/cookies.html)。以下Cookie將在一小時後過期(直接取自:How to set a cookie to expire in 1 hour in Javascript?)。

var now = new Date(); 
var time = now.getTime(); 
time += 3600 * 1000; 
now.setTime(time); 
document.cookie = 
    'username=' + value + 
    '; expires=' + now.toGMTString() + 
    '; path=/'; 

如果你想在模式進行下一次用戶訪問該頁面(或刷新頁面)介紹,你可以存儲在JavaScript的信息(或解除綁定觸發模式的情況下)。只需設置一個布爾值在JavaScript(注意範圍):

var modalDismissed = false; 

,並在點擊事件,設置爲true:

jQuery('.float_close').click(function() { 
    jQuery('.float_notice').fadeToggle('slow'); 
    modalDismissed = true; 
}); 

然後檢查,以確保modalDismissed是假彈出的前modal:

if (!modalDismissed) { 
    // modal 
}