Cookies in Safari

I am a novice at this, so pardon if this is a stupid question. Does safari need a special way to set/read cookies?

IFAIK, I should be able to set a cookie by using document.cookie=“name=value;” and display it simply with alert(document.cookie). This doesnt seem to work with safari tho; i don’t know if its not setting the cookie or just failing to display. does Safari require a special way of reading or setting cookies?

PS
I did check my browser to make sure it was set to ACCEPT cookies.

1 Like

No - are you sure you have JS enabled? Post the code.

Js is enabled…

the code is as follows

		function setExpire(hours){
		    var dayt = new Date ( );
				dayt.setTime(dayt.getTime()+(hours*60*60*1000));	
				return 	dayt;	    
		 		}
		 		
		//////////		
		function set_Cookie(name, value, expire, path, domain, secure){
			var CookieString=name + "=" + escape(value);				
			if (expire){
				exp=setExpire(expire);
				CookieString+= "; expires="+exp;
			} 
			if (path){
				CookieString+= "; path="+escape(path);
			}
			if (domain){
				CookieString+= "; domain="+escape(domain);
			}
			if (secure){
				CookieString+= "; secure";
			}
			document.cookie = CookieString;
		}
		///////
		function get_cookie (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 unescape( c.substring(nameEQ.length,c.length));
			}
			return null;
		}
		//////		
		function check_cookie(check, query){
			if ( ! get_cookie ( check ) ){
				 var value = prompt(query, "" );
				 set_Cookie(check, value,4, false, false, false);				
			}
			  var username = get_cookie ( "username" );
			  document.write ( "Hi " + username + ", welcome to my website!" );
			  document.write ( "<br><a href=\\"javascript:eraseCookie('username'); document.location.reload( );\\">Forget about me!</a>" );
		}
		////////
		function eraseCookie(name) {
				set_Cookie(name,"",-1,false,false,false);
		}
	check_cookie("username","who dat?");

Behaviour is consistent under Safari 4.0 (Win)

in safari 3.31 (mac) it’s always responding “NULL” as the vallue of the cookie? :frowning:

Before checking your code further, does this implementation of your code work on your Mac?

If not does this work?

nope …it’s still not working…? what did you change?

I didn’t change anything except document.location to window.location as it should be, but does the other page work?

You may have javascript enabled, but do you have cookies enabled?

Dave,
I think I do. I have cookies:accept" checked in my preferences, I don’t know if I needed anything else

As I don’t have a mac, and my Safari version is 4.0, I’m not 100% sure the settings are in the same place, or even if they look the same, but my settings for cookies are as follows:

1.) Location: Settings(the little “gear” button) > Preferences > Security (middle of the panel)
2.) Options: Always, Never, and “Only from sites I visit” (disables 3rd party cookies)

I have mine set for the third option. I’m not sure if this helps, but I’m hoping it does. :smiley:

yeah, dave… I have the same options , mine are set to always. I still dont quite get whats going on… but thank you :slight_smile: