Jquery对话框应该只对每个用户打开一次

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

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

Sample text here!

任何帮助深表感谢! 谢谢!

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

一种使用cookies的方式:

 $(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 ); } } }); } }); 

您必须检查用户是否访问过特定页面。 您可以将状态保存在会话变量或cookie中。 根据值,您可以显示对话框。

使用cookie的示例:在html文件中包含jquery cookie,即https://code.google.com/p/cookies/downloads/list

以下代码将完成您的任务

 $(document).ready(function() { $(function() { if ($.cookie('page_visited') != 'yes') { $( "#dialog-message" ).dialog({ modal: true, resizable: false, show: 'slide', buttons: { Ok: function() { $( this ).dialog( "close" ); } } }); $.cookie('page_visited','yes') } }); }); 

您可以将Cookie过期时间设置为第三个参数$.cookie('page_visited','yes', expiration_date)