aboutcookies

Using Cookies on your site

Writing Cookies on  your site

We explain in easy to understand terms, how you can write cookies

More

Reading Cookies you have set

An easy to follow guide to reading cookies you have set

More

Deleting Cookies you have set

Cookies cannot be deleted directly from a web page, you have to ‘expire’ them so that the browser can delete them for you

More

aboutcookies © 2012 - Fortune Design by RichoSoft - All Rights Reserved | Terms of Use | Privacy Policy | Made by RichoSoft

Reading Cookies

Here we will give a basic overview on how to read a cookie from a page on your site, that you can then use the value of in your javascript code.

Javascript: In this example we will use javascript to read the cookie.

Place this script in the <head> section of your page:

Then you can use the following code to read the cookie:

You can use the code above to read a cookie when the page loads, when a user clicks a button, or within other javascripts.

Examples:

<body onload=”var MyCookieValue = ReadCookie("MyCookie");”> would read a cookie called ‘MyCookie’ and place the value of the cookie in a variable called MyCookieValue when the page loads.

You could also add an onclick event to a button, image etc. like:

<Img src=”myphoto.jpg” onclick=”var MyCookieValue = ReadCookie("MyCookie");” />

Inside other javascripts you would simply add the  var MyCookieValue = ReadCookie("MyCookie"); within your other script.

You can only get the one parameter when reading the cookie and that is the Value of the Cookie and you will need to know the cookie’s name to read the cookie.

The expiry date of the cookie is not accessible when reading the cookie, you can only find out if the cookie exists  or not. If the Cookie has expired the browser will have removed it so it will not exist. If it has not expired then it will exist.

You can check if the cookie exists with something like:

var strcookie = ReadCookie("MyCookie");

if (strcookie.length == 0) { Do something }    (if the cookie length is 0, therefore is not there,  then do 1 thing if not do the other thing)

else

{Do something else}

You can also read cookies with ASP and PHP scripts, you do have some extra options with these, for example you use Keys, which can save and read more than one value using Name Value pairs. (PHP scripts not covered here at present)

Example:

To read the user name and password in a single cookie called UserInfo in ASP that has Keys, use something like:

strUserName = Request.Cookies(“UserInfo”)(“UserName”)        (The UserName is the Name of the Key)

strPassWord = Request.Cookies(“UserInfo”)(“PassWord”) (The PassWord is the Name of the Key)

You can then use the values of the variables in your ASP scripts. You will need to know the Key names and Cookie Name to read the keys directly like this.

Cookies