在页面加载时显示启用cookie的Jquery通知消息

时间:2010-12-17 22:42:28

标签: jquery cookies notifications message

我已经通过论坛阅读了许多与我的问题有关的帖子,但我找不到我想要的答案。

我想在页面加载时向用户显示带有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用户升级其浏览器

3 个答案:

答案 0 :(得分: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>

答案 1 :(得分:0)

here获取cookie函数,然后您可以将alertBox设置为默认隐藏,并且:

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

答案 2 :(得分:0)

相关问题