Set cookie even after HTML output

Submitted by:Jhon Jhon

Date added:31 December, 2014


Function that allows a Javascript cookie to be set after HTML has been output to the page.

Tags: set cookie , javascript cookie

Code Snippet:

< s c r ipt type="text/javascript">

FUNCTION JSSetCookie($CName,$CValue,$CExpr=FALSE){
// $CName is the name of the cookie
// $CValue is the value to be stored in the cookie
// $CXpr is in years
// This function will allow cookies to be set even after
// HTML has been printed on the page. It is a supplement to the
// SetCookie function when you have to output text before a cookie is set.

$CookieString="astr= '$CName' + '=' + '$CValue' + ';expires=' + expr + ';path=/';";
$Cookie.="\n<script language=\"javascript\">\n";
$Cookie.='function makeYearExpDate(yr){
var expire = new Date();
expire.setTime(expire.getTime() + ((yr *365) *24 * 60 * 60 * 1000));
expire = expire.toGMTString()
return expire
expr =makeYearExpDate('.$CExpr.');';
$Cookie.="\n<script language=\"javascript\">\n";
echo $Cookie;