问题
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