![](/img/trans.png)
[英]How do I open a modal/ dialog from a button from nav bar component in every page react
[英]Do not open modal on every page
我已經開發了運行良好的代碼。
問題是,即使我關閉它,它也會加載到每個頁面上。 有沒有一種方法可以保持關閉一定時間(可能基於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>
將此方法添加到您的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});
}
});
有關函數createCookie
和getCookie
更多信息,請getCookie
此處 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.