SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast xarzu's Avatar
    Join Date
    Apr 2007
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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:
    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:
    Code:
    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?

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,684
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    These are the standard functions to work with cookies, from the quirksmode cookies article:

    Code javascript:
    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);
    }
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •