Site Search:

How to add a client side database to blogger in 5 minutes


 ===================

<Back

Browser cookies have a very specific purpose: they are meant to store tiny bits of data and to pass that data to the web server on every request. Because they must be uploaded to the server along with every single request the browser makes, browsers limit cookies to a maximum size of 4 kilobytes per domain. Given this storage limitation and the fact that even a 4KB cookie is absolutely guaranteed to increase client/server latency, you’d think web developers would avoid packing cookies full of data.

If you just need to store a small set of data for blogger.

You can use cookie to store name value pairs, which is a cheap database. paste the code into your post

<script>

function setCookie(cname,cvalue,exdays) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays*24*60*60*1000));
    var expires = "expires=" + d.toGMTString();
    document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}

function getCookie(cname) {
    var name = cname + "=";
    var decodedCookie = decodeURIComponent(document.cookie);
    var ca = decodedCookie.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 storeValue() {
    var cname = prompt("Please enter variable name to store:","");
    var answer = prompt("Please enter variable value to store:","");
    if (cname != "" && cname != null && answer != "" && answer != null) {
       setCookie(cname, answer, 30);
    } else {
       alert(cname + " : " + answer);
    }
}

function deleteValue() {
    var cname = prompt("Please enter variable name to delete:","");
    if (cname != "" && cname != null) {
       setCookie(cname, "", -30);
    }
}

function showCookie() {
    var cname = prompt("Please enter value to display:","");
    var answer=getCookie(cname);
    if (answer != "") {
       alert(cname + " is " + answer);
    } else {
       alert("no such cookie: " + cname);
    }
}
</script>

<button type="button" onclick="storeValue()">add one</button>
<button onclick="deleteValue()">delete one</button>
<button onclick="showCookie()">show one</button >