每个会话一次向IE6-8用户显示警告

时间:2013-02-10 21:03:17

标签: javascript jquery internet-explorer

我环顾四周,找不到确切的解决方案,但如果确实存在,我道歉。

现在,我遇到的问题是每个会话向旧版Internet Explorer的用户显示一次div,即< IE9。

这是我快速整理的代码,但它似乎没有用,有人可以告诉我它有什么问题吗?我做错了吗?

<!--[if lt IE 9]>
<script>
    /* Set initial cookie after entire page has loaded */
    document.cookie="messageseen=true";

    /*********** 
       This cookie won't exist initially, but when the if statement below checks
       for the message seen cookie, the clear message cookie will also be set,
       this is so on subsequent page-loads, it won't take 5 seconds for the the
       message to disappear. 
    ***********/
    if (document.cookie == "clearmessage") {
       $('.ie-message').hide(); // hide message without delate
    }

    /*********** 
       After 5 seconds, check for the messageseen cookie, if it exists, set the
       clearmessage cookie and hide .ie-message. On all subsequent page-loads,
       the message will be hidden immediately without the delay.
    ***********/
    setTimeout(function (){
       if (document.cookie == "messageseen") {
          document.cookie="clearmessage=true";
          $('.ie-message').hide(); // hide message if cookie is set
       }
    }, 5000); // delay the hiding of the message for 5 seconds
</script>
<![endif]-->

2 个答案:

答案 0 :(得分:0)

使用

https://github.com/carhartl/jquery-cookie

<!--[if lt IE 9]>
<script>
    /* Set initial cookie after entire page has loaded */
    $.cookie("messageseen",true);

    /*********** 
       This cookie won't exist initially, but when the if statement below checks
       for the message seen cookie, the clear message cookie will also be set,
       this is so on subsequent page-loads, it won't take 5 seconds for the the
       message to disappear. 
    ***********/
    $('.ie-message').toggle(!$.cookie("clearmessage")); // hide message without delate

    /*********** 
       After 5 seconds, check for the messageseen cookie, if it exists, set the
       clearmessage cookie and hide .ie-message. On all subsequent page-loads,
       the message will be hidden immediately without the delay.
    ***********/
    setTimeout(function (){
       if ($.cookie("messageseen")) {
          $.cookie("clearmessage",true);
          $('.ie-message').toggle(0); // hide message if cookie is set
       }
    }, 5000); // delay the hiding of the message for 5 seconds
</script>
<![endif]-->

答案 1 :(得分:0)

如果你只有一个cookie,你可以这样做: (请注意,我隐藏div内联以防止在慢速页面加载时闪烁)

<!--[if lt IE 9]>
<div class"ie-message" style="display:none;">IE MESSAGE</div>
<script>
(function($){
  if ( document.cookie == "" ) {
  // show message
  $('.ie-message').show();
  // add cookie;
  document.cookie = "seen=true";
}
</script>
<![endif]-->