2015-08-08 56 views
0

我開發了這個運行良好的代碼。不要在每個頁面上打開模式

問題是,即使關閉它,它也會加載到每個頁面上。有沒有辦法,也許基於cookie,保持關閉一段時間?

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js" type="text/javascript"></script> 
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js" type="text/javascript"></script> 
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/themes/base/jquery-ui.css" type="text/css" media="all" /> 

<script type="text/javascript">           
$(document).ready(function() {       
$("#ModalMessage").dialog({modal: true, autoOpen : true}); 
}); 
</script> 

回答

0

將此方法添加到您的HTML文件中。

var createCookie = function(name, value, days) { 
    var expires; 
    if (days) { 
     var date = new Date(); 
     date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000)); 
     expires = "; expires=" + date.toGMTString(); 
    } 
    else { 
     expires = ""; 
    } 
    document.cookie = name + "=" + value + expires + "; path=/"; 
} 

function getCookie(c_name) { 
    if (document.cookie.length > 0) { 
     c_start = document.cookie.indexOf(c_name + "="); 
     if (c_start != -1) { 
      c_start = c_start + c_name.length + 1; 
      c_end = document.cookie.indexOf(";", c_start); 
      if (c_end == -1) { 
       c_end = document.cookie.length; 
      } 
      return unescape(document.cookie.substring(c_start, c_end)); 
     } 
    } 
    return ""; 
} 

然後,您可以使用像這樣的關閉對話框:

createCookie("closed", "1", 30); 

在加載頁面:

$(document).ready(function() {       
    if(getCookie("closed") != ""){ 
     //show my dialog box; 
     $("#ModalMessage").dialog({modal: true, autoOpen : true}); 
    } 
}); 

更多功能createCookiegetCookiehere