问题描述
我试图设置一个cookie,取决于我在我的Html中选择的css文件。我有一个带有选项列表的表单,以及不同的css文件作为值。当我选择一个文件,它应该被保存到一个cookie大约一个星期。下次打开html文件时,应该是您选择的上一个文件。
I'm trying to set a cookie depending on which css file i choose in my Html. I have a form with a list of options, and different css files as values. When I choose a file, it should be saved to a cookie for about a week. The next time you open your html file, it should be the previous file you've chosen.
JavaScript代码:
JavaScript code:
function cssLayout() {
document.getElementById("css").href = this.value;
}
function setCookie(){
var date = new Date("Februari 10, 2013");
var dateString = date.toGMTString();
var cookieString = "Css=document.getElementById("css").href" + dateString;
document.cookie = cookieString;
}
function getCookie(){
alert(document.cookie);
}
HTML代码:
<form>
Select your css layout:<br>
<select id="myList">
<option value="style-1.css">CSS1</option>
<option value="style-2.css">CSS2</option>
<option value="style-3.css">CSS3</option>
<option value="style-4.css">CSS4</option>
</select>
</form>
推荐答案
我发现下面的代码要比任何东西简单得多else:
I find the following code to be much simpler than anything else:
function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days*24*60*60*1000));
var expires = "; expires=" + date.toUTCString();
}
else var expires = "";
document.cookie = name + "=" + value + expires + "; path=/";
}
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
function eraseCookie(name) {
createCookie(name,"",-1);
}
现在,调用函数 b
$ b
Now, calling functions
createCookie('ppkcookie','testcookie',7);
var x = readCookie('ppkcookie')
if (x) {
[do something with x]
}
来源 -
他们今天更新了网页,因此网页中的所有内容都应该是最新的。
They updated the page today so everything in the page should be latest as of now.
这篇关于设置cookie和获取cookie与JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!