Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
278 views
in Technique[技术] by (71.8m points)

javascript - 设置Cookie并使用JavaScript获取Cookie [重复](Set cookie and get cookie with JavaScript [duplicate])

This question already has an answer here:

(这个问题已经在这里有了答案:)

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">CSS4</option>
    </select>
</form>
  ask by DrWooolie translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I find the following code to be much simpler than anything else:

(我发现以下代码比其他任何代码都简单得多:)

function setCookie(name,value,days) {
    var expires = "";
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days*24*60*60*1000));
        expires = "; expires=" + date.toUTCString();
    }
    document.cookie = name + "=" + (value || "")  + expires + "; path=/";
}
function getCookie(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) {   
    document.cookie = name+'=; Max-Age=-99999999;';  
}

Now, calling functions

(现在,调用函数)

setCookie('ppkcookie','testcookie',7);

var x = getCookie('ppkcookie');
if (x) {
    [do something with x]
}

Source - http://www.quirksmode.org/js/cookies.html

(来源-http: //www.quirksmode.org/js/cookies.html)

They updated the page today so everything in the page should be latest as of now.

(他们今天更新了页面,因此页面中的所有内容都应该是最新的。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...