[英]Javascript Timer / Bootstrap / Wordpress
I am using this little bit of code from Twitters Bootstrap in my Wordpress theme. 我在Wordpress主题中使用了Twitters Bootstrap的这段代码。 Is there a way that when the user closes the window it stays closed even after a refresh?
有没有一种方法,当用户关闭窗口时,即使刷新后窗口仍保持关闭状态? I need the window to stay closed for the rest of the session or at least an hour.
我需要窗口在整个会话的剩余时间或至少一个小时内保持关闭状态。
As it stands the window re-apears after a refresh. 在刷新后,窗口将重新出现。
<div class="alert alert-note alert-dismissible fade in" role=alert>
<button type=button class=close data-dismiss=alert aria-label=Close>
<span aria-hidden=true>×</span>
</button>
<h2>Upgrade to Pro for £2</h4>
<p>Upgrade to a voclr.it Pro Account for just £2 a month and unlock many great features.</p>
<div class="row">
<div class="col-sm-4">
<h4>Previews</h4>
<img src="<?php bloginfo('template_url') ?>/img/start4.png" alt="start4" width="64" height="64">
</div>
<div class="col-sm-4">
<h4>Premium Sample Packs</h4>
<img src="<?php bloginfo('template_url') ?>/img/pulse2.png" alt="pulse2" width="64" height="64">
</div>
<div class="col-sm-4">
<h4>No More Adverts</h4>
<img src="<?php bloginfo('template_url') ?>/img/forbidden.png" alt="forbidden" width="64" height="64">
</div>
</div>
</div>
Here's an example using local storage sessionStorage to be more precise. 这是使用本地存储sessionStorage的示例,更精确地说。
https://jsfiddle.net/Pigbot/dwbynr6y/ https://jsfiddle.net/Pigbot/dwbynr6y/
//Check if 'hidden' variable is defined
if (typeof (hidden) !== 'undefined') {
// If the 'hidden' variable is defined don't do anything
} else {
// If the 'hidden' variable is NOT defined
// Check if browser supports Local Storage
if(typeof(Storage) !== "undefined") {
// Set the 'hidden' variable to the value of the local storage item
var hidden = sessionStorage.getItem("theDiv");
// If the 'hidden' variable is set to the string 'hidden' the hide the div
if(hidden === "hidden"){
$('.alert-dismissible').hide();
}
//If the 'hidden' variable is not set to the value of the local
//storage item then the div will be visible and so will the button
//Click function on buttom to set the local storage and fadeout the div
$('button.close').click(function(){
sessionStorage.setItem("theDiv", "hidden");
hidden = sessionStorage.getItem("theDiv");
$('.alert-dismissible').fadeOut();
});
} else {}
}
//This button is an example of removing the item from local storage
$('button.clear').click(function(){
sessionStorage.removeItem("theDiv");
});
window.localStorage - stores data with no expiration date window.localStorage-存储没有到期日期的数据
window.sessionStorage - stores data for one session (data is lost when the browser tab is closed) window.sessionStorage-存储一个会话的数据(关闭浏览器选项卡时数据丢失)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.