Cookie value to define style on page load

Posted by zac on Stack Overflow See other posts from Stack Overflow or by zac
Published on 2010-05-04T05:40:24Z Indexed on 2010/05/04 5:48 UTC
Read the original article Hit count: 223

Filed under:
|

I am using the scripts from here http://www.quirksmode.org/js/cookies.html and have successfully created a cookie.. but am having trouble doing anything with it. I would like to have a style defined if a cookie is present. The function for the readCookie is

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

I am trying to use it on page load with something like this

window.onload=function(){
   var x = readCookie('myCookieValue');
   if (x)  {    
   document.getElementById('div').innerHTML = "<style type=\"text/css\">.form {display:none}</style>";
   }
}

What would be the correct way of writing this?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about cookies