How To Access Cookie Without any special Charecters

Generally, when we access javascript, wild card charecters are replaced by some special symbols (like +,% etc). This code is for accessing cookie value withour any special chacters. Try It.

//Reading Cookie

function ReadCookie(n) {
var cookiecontent = new String();
if(document.cookie.length > 0) {
 var cookiename = n+ ‘=’;
 var cookiebegin = document.cookie.indexOf(cookiename);
 var cookieend = 0;
 if(cookiebegin > -1) {
  cookiebegin += cookiename.length;
  cookieend = document.cookie.indexOf(“;”,cookiebegin);
  if(cookieend < cookiebegin) { cookieend = document.cookie.length; }
  cookiecontent = document.cookie.substring(cookiebegin,cookieend);
 
  }
 }
return unescape(cookiecontent);
} //  This Code is for reading cookie value. But the output will consist of  ‘+’ sign in the place wild card charecters. To remove this ‘+’

//use the following function.

function get_body()
 {
  var ind = document.form2.lstmessage.selectedIndex;
      var string=ReadCookie(ind);
      var strReplaceAll = string;
      var intIndexOfMatch = strReplaceAll.indexOf( “+” );
 
      // Loop over the string value replacing out each matching
      // substring.
      while (intIndexOfMatch != -1){
      // Relace out the current instance.
       strReplaceAll = strReplaceAll.replace( “+”, ” ” );
 
      // Get the index of any next matching substring.
      intIndexOfMatch = strReplaceAll.indexOf( “+” );
      }
 
      //alert( strReplaceAll );
      document.form2.txtbody.value = strReplaceAll;    
 }
Cookies in Javascript

/ _________________Function To Create Cookie In 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 To Read Cookie In Javascript____________________ //
 
function ReadCookie(n) {
  var cookiecontent = new String();
 if(document.cookie.length > 0) {
 var cookiename = n+ ‘=’;
 var cookiebegin = document.cookie.indexOf(cookiename);
 var cookieend = 0;
 if(cookiebegin > -1) {
  cookiebegin += cookiename.length;
  cookieend = document.cookie.indexOf(“;”,cookiebegin);
  if(cookieend < cookiebegin) { cookieend = document.cookie.length; }
  cookiecontent = document.cookie.substring(cookiebegin,cookieend);
 
  }
 }
return unescape(cookiecontent);
} // function ReadCookie()
 
Note : If The cookie contents consist of some ‘+’ symbol then use the following code to filter the cookie contect. The
Following function is to remove the + symbols from the cookie content. It can be used for removing other special charecters from the cookie content. To use the following code to remove other special charecters, simple replace the ‘+’ symbol by the special symbol in star(//***********************//) marked line..
 
      var string=ReadCookie(ind);
      var strReplaceAll = string;
      var intIndexOfMatch = strReplaceAll.indexOf( “+” ); //***********************//
 
      // Loop over the string value replacing out each matching
      // substring.
      while (intIndexOfMatch != -1){
      // Relace out the current instance.
       strReplaceAll = strReplaceAll.replace( “+”, ” ” );
 
      // Get the index of any next matching substring.
      intIndexOfMatch = strReplaceAll.indexOf( “+” );
      }
 
 
Author – Dhiraj Das
From – Skillhut