Question: ❓❓❓


This question already has an answer here:

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 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 code:

<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>


Good Answer: 👌👌👌

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

function createCookie(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 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);
}

Now, calling functions

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

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

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

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


Read More: 👉 https://stackoverflow.com/questions/14573223/set-cookie-and-get-cookie-with-javascript