Jquery对话框应该只对每个用户打开一次 [英] Jquery dialog should only open once per user

查看:75
本文介绍了Jquery对话框应该只对每个用户打开一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在首页上使用jquery ui模式对话框,我想每个用户只显示一个。这是我的代码:

I use jquery ui modal dialog on a front page and I'd like to show it only one per user. This is my code:

  <script>
  $(function() {
    $( "#dialog-message" ).dialog({
      modal: true,
      resizable: false,
      show: 'slide',
      buttons: {
        Ok: function() {
          $( this ).dialog( "close" );
        }
      }
    });
  });


  </script>



<div id="dialog-message" title="Attention">
  <p>Sample text here!</p>
</div>

非常感谢任何帮助!
谢谢!

Any help is much appreciated! Thanks!

推荐答案

使用 cookies (如前所述)或 localStorage API (取决于您必须支持的浏览器) )。

Either use cookies (like mentioned before) or the localStorage API (depending on which browsers you have to support).

使用Cookie的方法:

$(function() {
    if( document.cookie.indexOf( "runOnce" ) < 0 ) {
        $( "#dialog-message" ).dialog({
            modal: true,
            resizable: false,
            show: 'slide',
            buttons: {
                Ok: function() {
                    $( this ).dialog( "close" );
                    document.cookie = "runOnce=true; expires=Fri, 31 Dec 9999 23:59:59 GMT; path=/";
                }
            }
        });
    }
});

使用localStorage的方式:

$(function() {
    if( ! localStorage.getItem( "runOnce" ) ) {
        $( "#dialog-message" ).dialog({
            modal: true,
            resizable: false,
            show: 'slide',
            buttons: {
                Ok: function() {
                    $( this ).dialog( "close" );
                    localStorage.setItem( "runOnce", true );
                }
            }
        });
    }
});

这篇关于Jquery对话框应该只对每个用户打开一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆