将Cookie设置为仅显示弹出窗口一次 [英] Setting a cookie to only show popup once
问题描述
我试图设置一个cookie只显示一个弹出窗口,这里是我的代码到目前为止:
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
});
}
});
目前这个加载每次parent-page在URL中,我需要只显示一次。
Currently this loads every time parent-page is in the URL, I need to only show it once. How can I do this?
推荐答案
您可以使用 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
}
});
localStorage属性允许您访问本地存储对象。 localStorage类似于sessionStorage。唯一的区别是,虽然存储在localStorage中的数据没有到期时间,但是在浏览会话结束时,即浏览器关闭时,存储在sessionStorage中的数据将被清除。
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设置为仅显示弹出窗口一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!