Cookies in Javascript: Handling Session Information

Cookies written using Javascript is a technique that enables a wensite owner to store information about a given user for submission in a form, or so manipulation at a later browsing session by the same use. Being that cookies are stored on the user's machine (client-side), they do not present a fail-safe way to handle information, but they are essential in accomplishing this task in situations where the web programmer does not have access so server-side programming and scripting plateforms such as ASP, JSP and PHP (just to mention a few).

The following code was created to collect browser information such as the 'referrer' and 'landing page' of a given user session and include them as hidden fields in a possible form submission.

Points to note:
In order to track this information from more than on page but avoid overwriting already collected information, it is necessary to create an extra data field that contains a flag to indicate whether or not the same user has collected a cookie within a given period in time.

It was decided that once the user submits a feewback form that attaches the referrer and landing page information, the cookies will not have any use and should be deleted to enable tracking a new browsing session. Or if the browsing session lasted more than 2 hours, then the cookies would expire to make it possible to track a new session (assuming that after two hours of inactivity the user will probably conduct a new web-search and probably arrive that the web-page in a new way).

The following code serves to collect the referrer (SEM source) and landing page information and attach them to every feedback record.

The main Javascript file is attached to this document:
- Every page makes a call to the javascript source and checks the status flags before writing a new cookie, or resetting the expiry time so that people surfing for more than the alloted time do nto loose their information.

<script language="JavaScript" type="text/javascript" xsrc="trackme.js"></script><script language="JavaScript" type="text/javascript"><!--
trackMe();
//-->
</script>

Once the user arrives at the submission form, there is ASP code to collect  the cookie information and add it to the form as hidden fields for submission. Although Javascript could also be used to accomplish this, whever possible (if there is server-side technology available), it's quicker to accomplish the fact as such.

<%
Response.write("<input type='hidden' name='my_referrer' value='"& Request.Cookies("my_referrer")&"'>")Response.write("<input type='hidden' name='my_entry' value='"& Request.Cookies("my_entry")&"'>")
%> 

After the cookie information is loaded into the frm and the form submitted, the POST destination for the form has code that sets the cookie expiry date in the past and also uses ASP to abandon the browser session <Session.Abandon%> to make it possible for the user to browse with new variables.

This task could have been better handled using Server-side scripts which do not suffer from the short-coming of cookies (they might be blocked by the user, deleted or overwritten). However, in some situations (especially on web-paths using legacy technology/extensions), browser scripting may be the only way out. 

 

AttachmentSize
trackme.js.txt2.96 KB
Valid XHTML 1.0 Strict
This site is accepts Oped ID authentication for login
This Website is Built Using Semantic Markup and Cascading Style Sheets (CSS)
Some usage rights are reserved, please contact us for approval before using it