Why doesn't "document.cookie = 'thecookie=; expires=; path=/;';" make a cookie in IE?

I don’t understand why

document.cookie = 'thecookie=; expires=; path=/;';

sets an empty cookie (called “thecookie” that expires when the browser closes and is accessible via any directory on the website)
in all the browsers I have tried apart from IE7?


ro0bear :smiley:

Shot in the dark:

I don't know how many cookies you are using but, IE7 only allows 20 cookies per domain..

Don’t forget to put in empty quotes for the unused parameters or you’ll get an error when you run the code.

Hope it helps.

Thanks, this seems to work perfectly in all browsers…

document.cookie = 'thecookie=""; path=/';

Now to fix the other IE bugs, agh! :headbang: You would have thought Microsoft deliberately made it difficult for developers!

Thanks for your help :slight_smile:

This should help you then. Some proper cookie handling functions from http://www.quirksmode.org/js/cookies.html

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		var expires = "; expires="+date.toGMTString();
	else var expires = "";
	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) {

Thanks Paul, the other IE problems are not problems with the cookies though unfortunately.

Much appreciated all the same though, thanks :slight_smile:

I will see if I can work out what’s wrong with it tomorrow.

I think in future when I write a program in JavaScript I will check both in Firefox and IE as I go, so I can fix problems as the occur rather than searching through looking for what’s wrong lol. (also writing functions to make solving cross browser problems in the future nice and easily).

Thanks again (:

You’ll find that it’s a lot easier to develop on something like Firefox and apply fixes for IE, than to do it the other way around where you develop for IE and apply fixes for Firefox.

It need not be Firefox of course, there’s also Google Chrome, Safari, Opera which are popular ones.

The issue is that there is a [http://en.wikipedia.org/wiki/Comparison_of_web_browsers vast number of web browsers] out there, so it’s best to develop from a common set of standards and then apply cross-browser techniques where required.