[英]Set cookie using cookie.js
I'm trying to make the page remember the event that was fired by clicking on a radio button using jquery.cookie.js but it's not saving the cookie. 我试图让页面记住使用jquery.cookie.js单击单选按钮时触发的事件,但它没有保存cookie。
Here's my code: 这是我的代码:
HTML: HTML:
<input type="radio" id="foo" name="langs" value="fooval">
<input type="radio" id="bar" name="langs" value="barval">
<span class="foo">Foo</span>
<span class="bar">Bar</span>
<script src="http://code.jquery.com/jquery.js"></script>
<script src="https://raw.githubusercontent.com/carhartl/jquery-cookie/master/src/jquery.cookie.js"></script>
JS: JS:
$(document).ready(function () {
$('input').change(
function () {
var closed = $('span').is(":hidden");
if ($(this).val() == "barval") {
$('.foo').fadeOut();
$('.bar').fadeIn();
} else {
$('.bar').fadeOut();
$('.foo').fadeIn();
}
setCookie("open", closed, 365);
});
var showsqux = getCookie("open");
if (showqux == "true") {
$('.foo').fadeOut();
$('.bar').fadeIn();
} else {
$('.bar').fadeOut();
$('.foo').fadeIn();
}
});
function setCookie(c_name, value, exdays) {
var exdate = new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value = escape(value) + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString());
document.cookie = c_name + "=" + c_value;
}
function getCookie(c_name) {
var i, x, y, ARRcookies = document.cookie.split(";");
for (i = 0; i < ARRcookies.length; i++) {
x = ARRcookies[i].substr(0, ARRcookies[i].indexOf("="));
y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);
x = x.replace(/^\s+|\s+$/g, "");
if (x == c_name) {
return unescape(y);
}
}
}
CSS: CSS:
.bar {
display:none
}
Where am I going wrong? 我要去哪里错了?
var show s qux var show s qux
var showsqux = getCookie("open");
if (showqux == "true") { 如果(showqux ==“ true”){
if (showqux == "true") {
Darn those typos. 那些错别字。 :P :P
Try to steer clear of using cookies, as it's an outdated standard. 尝试避免使用cookie,因为它已经过时了。
WebStorage is the new HTML5 standard which I would recommend and you can here store data of up to 10mb either permanently or simply for that session of viewing. WebStorage是我推荐的新HTML5标准,您可以在此处永久或简单地为该查看会话存储高达10mb的数据。
You can get & set data here with one of two simple commands: 'getItem' or 'setItem' 您可以使用以下两个简单命令之一获取和设置数据:“ getItem”或“ setItem”
See for more detail: http://www.w3schools.com/html/html5_webstorage.asp 请参阅以了解更多详细信息: http : //www.w3schools.com/html/html5_webstorage.asp
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.