设置 cookie 只显示一次弹出窗口
Setting a cookie to only show popup once
我正在尝试将 cookie 设置为仅显示一次弹出窗口,这是我目前的代码:
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
});
}
});
目前每次父页面在 URL 时都会加载,我只需要显示一次。我该怎么做?
您可以使用 jQuery cookie plugin 来实现:
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);
}
您可以使用 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.
文档:https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage
我正在尝试将 cookie 设置为仅显示一次弹出窗口,这是我目前的代码:
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
});
}
});
目前每次父页面在 URL 时都会加载,我只需要显示一次。我该怎么做?
您可以使用 jQuery cookie plugin 来实现:
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);
}
您可以使用 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.
文档:https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage