本文翻译自:Set cookie and get cookie with JavaScript [duplicate]
This question already has an answer here: 这个问题已经在这里有了答案:
- How do I create and read a value from cookie? 如何创建和读取Cookie中的值? 17 answers 17个答案
I'm trying to set a cookie depending on which CSS file I choose in my HTML. 我试图根据我在HTML中选择的CSS文件来设置Cookie。 I have a form with a list of options, and different CSS files as values. 我有一个带有选项列表的表单,以及不同的CSS文件作为值。 When I choose a file, it should be saved to a cookie for about a week. 当我选择一个文件时,应将其保存到Cookie大约一周。 The next time you open your HTML file, it should be the previous file you've chosen. 下次打开HTML文件时,它应该是您选择的上一个文件。
JavaScript code: JavaScript代码:
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 code: 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