我正在尝试根据我在HTML中选择的CSS文件来设置cookie.我有一个包含选项列表的表单,以及不同的CSS文件作为值.当我选择一个文件时,它应该保存到cookie大约一个星期.下次打开HTML文件时,它应该是您选择的上一个文件.
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);
}
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud) var curCookie = name + "=" + value +
"; expires=" + ATS_getExpire() +
"; path=" + path +
"; domain=" + domain ;
document.cookie = curCookie;
alert("Your Cookie : " + document.cookie);
Run Code Online (Sandbox Code Playgroud)
当我使用上面的代码时,警报消息将变为空.为什么document.cookie会变空.请有人回答.
我希望下面的代码设置root的路径,我知道我必须设置/作为路径值,但这不是我的代码,我不熟悉Javascript!
function setCookie(name, value, expires, path, domain, secure){
document.cookie= name + "=" + escape(value) +
((expires) ? "; expires=" + expires.toGMTString() : "") +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
((secure) ? "; secure" : "");
}
Run Code Online (Sandbox Code Playgroud)
我已经尝试过如下编辑代码但是没有成功.
setCookie(name, value, expires, path, domain, secure){
document.cookie= name + "=" + escape(value) +
((expires) ? "; expires=" + expires.toGMTString() : "") +
((path) ? "; path="/") +
((domain) ? "; …Run Code Online (Sandbox Code Playgroud)