Need help setting js cookie

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
someguyhere
Forum Contributor
Posts: 181
Joined: Sun Jul 27, 2008 3:24 pm

Need help setting js cookie

Post by someguyhere »

I'll preface this by saying I know NOTHING about javascript...

I've looked at tons of tutorials, but I clearly must be missing something, because I can't get any of them to work.

Code: Select all

<script type="text/javascript">

<!--
function WriteCookie()
{
   cookievalue= "813-653-1111" + ";";
   cookie="localnumber=" + cookievalue;
}
WriteCookie();
//-->

</script>

</head>

<body>

<script type="text/javascript">
	document.write(getCookie("localnumber"));
</script>
But it doesn't seem to be writing the cookie. Can someone tell me what I'm going wrong?
someguyhere
Forum Contributor
Posts: 181
Joined: Sun Jul 27, 2008 3:24 pm

Re: Need help setting js cookie

Post by someguyhere »

I've changed the code, and it works on the page now, but when navigating to other pages, it returns a value of null

Code: Select all

<!--
function setCookie()
{
	cookievalue= "<?php echo get_post_meta($post->ID, 'phone', true); ?>" + ";";
	document.cookie="localnumber=" + cookievalue;
}
setCookie();

	function getCookie(name)
{	
	var re = new RegExp(name + "=([^;]+)");
	var value = re.exec(document.cookie);
	return (value != null) ? unescape(value[1]) : null; 
}
getCookie();
//-->
Any ideas?
Post Reply