How can I share cookies between ColdFusion and JavaScript?

Sharing Cookies between ColdFusion and JavaScript is an easy way to pass data back and forth between the 2 technologies. A cookie is a cookie- regardless of if it was set by ColdFusion, Java, .net, or JavaScript. As long as you know the cookie name (including exact case in many situations) you can access and manipulate the cookie. With that said, the biggest trick to sharing cookies between ColdFusion and JavaScript is to remember that ColdFusion ignores case, but JavaScript does not. To use a JavaScript cookie in ColdFusion case does not matter. But to use a ColdFusion cookie in JavaScript, you need to reference the cookie name all in caps.

The following code is broken into two pages. The first page sets two cookies. The first cookie is set via JavaScript, and the second cookie is set via ColdFusion. The Second page then uses ColdFusion and JavaScript to display the contents of the cookie that was set by the other language:

<!--- Page1.cfm --->
<script language=javaScript>
//This function will set a JavaScript cookie
function setCookie(name, value) {
	//build an expiration time 1 hour into the future
	var expDate = new Date()
	expDate.setTime(ExpDate.getTime() + 60*60*1000);
	//set the cookie
  document.cookie = name + "=" + escape(value) + ";" + expDate.toGMTString();
setCookie('jSCookie', 'JavaScript!');
// -->
<cfcookie name="cFCookie" value="ColdFusion!" expires="never">
<!--- Page2.cfm --->
<script language=javaScript>
// This function will return the value of a JavaScript cookie
function getCookie(name) {
	//init output
  var output = null;
	//apend ; to end so we can calculate end of cookie text
  var myCookie = " " + document.cookie + ";";
	//append = to cookie name so any additional text is the cookie value
  var search = " " + name + "=";
	// init search start location
  var begin = myCookie.indexOf(search);
	//init search end location
  var end;
	//loop over cookie text and pull out the value we want
  if (begin != -1) {
    begin += search.length;
    end = myCookie.indexOf(";", begin);
    output = unescape(myCookie.substring(begin, end));
   return output;
// -->

This question was written by Jeremy Petersen
It was last updated on April 3, 2006.




comments powered by Disqus