﻿<?xml version="1.0" encoding="utf-8"?>
<snippet name="Get And Set A Cookie" description="Get And Set A Cookie" preview="code" type="block">
<insertText location="beforeSelection">
<![CDATA[function setCookie(cookieName, cookieValue, expdays) {
    var d = new Date();
    d.setTime(d.getTime() + (expdays*24*60*60*1000));
    var expires = "expires="+d.toUTCString();
    document.cookie = cookieName + "=" + cookieValue + "; " + expires;
}

function getCookie(cookieName) {
    var name = cookieName + "=";
    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);
        if (c.indexOf(name) == 0) return c.substring(name.length, c.length);
    }
    return "";
}

function checkCookie() {
    var username = getCookie("username");
    if (username != "") {
        alert("Welcome: " + username);
    } else {
        username = prompt("Please enter your user name:", "");
        if (username != "" && username != null) {
            setCookie("username", username, 365);
        }
    }
}]]>
</insertText>
<insertText location="afterSelection"><![CDATA[]]>
</insertText>
</snippet>
