admin管理员组文章数量:1335682
I'm trying to setup a cookie to only show a popup once, here's my code so far:
jQuery(window).load(function(){
// Load pop up within parent-page section only
if (window.location.href.indexOf('parent-page') > -1) {
alert("your url contains the parent-page in the URL");
$.magnificPopup.open({
items: [
{
src: '#disclaimer', // CSS selector of an element on page that should be used as a popup
type: 'inline'
}
],
removalDelay: 300,
mainClass: 'mfp-fade',
closeOnContentClick: false,
modal: true
});
}
});
Currently this loads every time parent-page is in the URL, I need to only show it once. How can I do this?
I'm trying to setup a cookie to only show a popup once, here's my code so far:
jQuery(window).load(function(){
// Load pop up within parent-page section only
if (window.location.href.indexOf('parent-page') > -1) {
alert("your url contains the parent-page in the URL");
$.magnificPopup.open({
items: [
{
src: '#disclaimer', // CSS selector of an element on page that should be used as a popup
type: 'inline'
}
],
removalDelay: 300,
mainClass: 'mfp-fade',
closeOnContentClick: false,
modal: true
});
}
});
Currently this loads every time parent-page is in the URL, I need to only show it once. How can I do this?
Share Improve this question edited Jun 17, 2015 at 9:39 Rory McCrossan 338k41 gold badges320 silver badges351 bronze badges asked Jun 17, 2015 at 9:38 egr103egr103 4,01815 gold badges71 silver badges120 bronze badges 2- As you said either by storing the seen state in a cookie or local storage! Do you have some code along that path? – lshettyl Commented Jun 17, 2015 at 9:43
- Depends do you want that it not appear for the current session or for x days? – Dekron Commented Jun 17, 2015 at 9:45
2 Answers
Reset to default 4You can use the jQuery cookie plugin to achieve this:
if (window.location.href.indexOf('parent-page') > -1 && !$.cookie('popup-shown')) {
$.magnificPopup.open({
items: [
{
src: '#disclaimer', // CSS selector of an element on page that should be used as a popup
type: 'inline'
}
],
removalDelay: 300,
mainClass: 'mfp-fade',
closeOnContentClick: false,
modal: true
});
$.cookie('popup-shown', true);
}
You can use localStorage
:
jQuery(window).load(function () {
if (window.location.href.indexOf('parent-page') > -1 && !localStorage.getItem('popup_show')) {
$.magnificPopup.open({
items: [{
src: '#disclaimer', // CSS selector of an element on page that should be used as a popup
type: 'inline'
}],
removalDelay: 300,
mainClass: 'mfp-fade',
closeOnContentClick: false,
modal: true
});
localStorage.setItem('popup_show', 'true'); // Set the flag in localStorage
}
});
The localStorage property allows you to access a local Storage object. localStorage is similar to sessionStorage. The only difference is that, while data stored in localStorage has no expiration time, data stored in sessionStorage gets cleared when the browsing session ends - that is when the browser is closed.
Docs: https://developer.mozilla/en-US/docs/Web/API/Window/localStorage
本文标签: javascriptSetting a cookie to only show popup onceStack Overflow
版权声明:本文标题:javascript - Setting a cookie to only show popup once - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742372697a2462546.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论