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> Code (markup): 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"; Code (markup): 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?
Add cookie function addCookie(szName,szValue,dtDaysExpires) { var dtExpires = new Date(); var dtExpiryDate = ""; dtExpires.setTime(dtExpires.getTime() + dtDaysExpires * 24 * 60 * 60 * 1000); dtExpiryDate = dtExpires.toGMTString(); document.cookie = szName + "=" + szValue + "; expires=" + dtExpiryDate; } Code (markup): Create cookie - addCookie("Count","0",10); Find cookie function findCookie(szName) { var i = 0; var nStartPosition = 0; var nEndPosition = 0; var szCookieString = document.cookie; while(i <= szCookieString.length) { nStartPosition = i; nEndPosition = nStartPosition + szName.length; if(szCookieString.substring( nStartPosition,nEndPosition) == szName) { nStartPosition = nEndPosition + 1; nEndPosition = document.cookie.indexOf(";",nStartPosition); if(nEndPosition < nStartPosition) nEndPosition = document.cookie.length; return document.cookie.substring( nStartPosition,nEndPosition); break; } i++; } return ""; } Code (markup): Example. If you want find a cookie with name "Visit" and place it in variable. var szVisitValue = findCookie("Visit"); Set cookie parameters addCookie("Count","0",10); //callback again with new parameters addCookie("Count","5",10); Delete Cookie function removeCookie(szName) { var dtExpires = new Date(); dtExpires.setTime(dtExpires.getTime() - 1); var szValue = findCookie(szName); document.cookie = szName + "=" + szValue + "; expires=" + dtExpires.toGMTString(); } Code (markup): Remove cookie removeCookie("Count");
I want to check and see if this Cookie is set when the page reloads. To do this I guess I want to call a javascript function in my html <BODY> tag during the loading of my web page. Is this done like this: <html> <body onload=somefunction.... or is it like this: <html> <body onload="somefunction.... or is it done some other way?