Trying to set a cookie in Chrome!

I have Chrome 16.0.912.63 m. I’m learning js. A simple cookie setting example script works in IE & Firefox but not in Chrome.

<a href="javascript: setcookie();">Set Cookie</a><br/>
<a href="javascript: readcookie();">Read Cookie</a>


<script type="text/javascript">

	function setcookie(){
		document.cookie="randomcookietext";
	}

	function readcookie(){
		alert(document.cookie);
	}


</script>

What do I need to do differently in Chrome?

Its not just Chrome. Here is how to properly handle cookies.


function createCookie(name, value, days) {
    var expires = '',
        date = new Date();
    if (days) {
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        expires = '; expires=' + date.toGMTString();
    }
    document.cookie = name + '=' + value + expires + '; path=/';
}

function readCookie(name) {
    var cookies = document.cookie.split(';'),
        length = cookies.length,
        i,
        cookie,
        nameEQ = name + '=';
    for (i = 0; i < length; i += 1) {
        cookie = cookies[i];
        while (cookie.charAt(0) === ' ') {
            cookie = cookie.substring(1, cookie.length);
        }
        if (cookie.indexOf(nameEQ) === 0) {
            return cookie.substring(nameEQ.length, cookie.length);
        }
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name, '', -1);
}

Thanks for that but it’s not as helpful as I’d hoped.

I began learning javascript a week or two ago. I’m reading Pollock’s Javascript - A Beginner’s Guide. It is just Chrome since my code does work in IE and Firefox. I’m sure I would eventually have come across the more updated/proper ways of handling cookies but for now I’m looking to find out why my code doesn’t work in Chrome. If it’s very important that I immediately abandon the way Pollock’s book is teaching me then I could do with some reasons why.

I’m trying to understand the code, I don’t want to simply memorize some code that works, I want to understand why it works better or why my code was faulty.

Google Chrome is much more secure than other web browsers. As a result of that, a local web page has no cookie privileges. It’s only if the page is running from a web server that it has permission to use cookies.

I see, thanks.

Opera used to be like that too, until we put-on pressure to get it changed.