Cookies with HTML and with IE8

Help Me With Cookies, HTML and IE8

For complex reasons, I have to use HTML with JavaScript to set a cookie. I know it is easy to set and get cookies PHP but I have to put the code in HTML.

I have searched the internet for some examples and I ran a test based on an example. I ran this Javascript code:


<SCRIPT LANGUAGE="JavaScript">

function putCookie()
{
	cookie_name = "specialcookiehuge";

 	if(document.cookie != document.cookie)
	{
		index = document.cookie.indexOf(cookie_name);
	}
	else
	{
		index = -1;
	}

	if (index == -1)
	{
		document.cookie=cookie_name+"; expires=Monday, 04-Apr-2020 05:00:00 GMT";
	}

}
</SCRIPT>


I tested this code by placing alert statements and I nkow that the line:

document.cookie=cookie_name+"; expires=Monday, 04-Apr-2020 05:00:00 GMT";

gets run.

I tried to find this cookie (Tools -> Internet Options -> Browsing History ->Settings) And I did not find anything named “specialcookiehuge” in “View Objects” or “View Files”.

I guess I could try other browsers, but it would be good to know what I am doing wrong. I do not know if I am really setting a cookie. Is there some additional javascript command I am missing? Or, is the cookie being set but I am not looking in the right place according to the tutorials I saw online?

These are the standard functions to work with cookies, from the quirksmode cookies article:


function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		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) {
	createCookie(name,"",-1);
}