Modal Popup Only once per session

試著忘記壹切 提交于 2021-02-10 20:42:06

问题


How to show this Dialog Popup one time per session, I see cookies configuration but I could not apply it to this case:

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

This is an example: CoverPop in context with my code, How do I apply?


回答1:


You can use sessionStorage (HTML 5) to keep a value that will let you know if you have already shown the popup.

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

You can modify your code in this parts:

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

Then you can validate in your document.ready everytime it gets called like this:

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

This should work, let me know if you have any questions about this approach.



来源:https://stackoverflow.com/questions/30269341/modal-popup-only-once-per-session

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!