模态弹出每个会话只有一次

时间:2015-05-15 21:55:33

标签: jquery html css popup modal-dialog

如何在每个会话中显示一次此对话框弹出窗口,我看到了cookie配置,但我无法将其应用于此案例:

$(document).ready(function() {
    ShowDialog(true);
    e.preventDefault();
});

$("#btnClose").click(function(e) {
    HideDialog();
    e.preventDefault();
});

function ShowDialog(modal) {
    $("#overlay").show();
    $("#dialog").fadeIn(300);

    if (modal) {
        $("#overlay").unbind("click");
    } else {
        $("#overlay").click(function(e) {
            HideDialog();
        });
    }
}

function HideDialog() {
    $("#overlay").hide();
    $("#dialog").fadeOut(300);
}

这是一个示例:CoverPop在我的代码的上下文中,我如何申请?

1 个答案:

答案 0 :(得分:6)

您可以使用sessionStorage(HTML 5)保留一个值,以便在您显示弹出窗口时通知您。

http://www.w3schools.com/Html/html5_webstorage.asp

您可以在以下部分修改您的代码:

 $("#btnClose").click(function (e)
  {
     HideDialog();
     e.preventDefault();
     sessionStorage["PopupShown"] = 'yes'; //Save in the sessionStorage if the modal has been shown
  });

然后你可以在你的document.ready每次被调用时进行验证:

$(document).ready(function ()
{
     if(sessionStorage["PopupShown"] != 'yes'){ 
         ShowDialog(true);
         e.preventDefault();
     }
});

这应该有用,如果您对此方法有任何疑问,请与我联系。