views:

37

answers:

3

Hi,

I am using the following plug in for cookies in jQuery:

https://code.google.com/p/cookies/

The issue i am having is not with the plugin but when and how to delete the cookie at the end of a quoting process.

The site i am using this on is a six step online quote and buy process.

There is Omniture event serialisation sitestat tracking applied to some of the pages. This event serialisation has to include the name of the event and a random number of which i create.

I have a generic function for this which i call at the bottom of the page like so:

serialEvent('event21:', 'payment');

Here is the function:

function serialEvent(eventNumber, eventName) {
    var sessionID = jaaulde.utils.cookies.get('sessionID');             
    var remLength = 20 - eventName.length;  
    var remSession = sessionID.substr(sessionID.length - remLength, remLength);
    var eventName = eventName + remSession;
    s.events = eventNumber + eventName; 
    }

I need to delete the cookie at the end of the process, the Thank you page but i also need the cookie 'sessionID' for the 'serialEvent' function.

As the function is called at the bottom of the page should i just write the cookie delete after it? Is that robust enough?

I need to be sure that the function has successfully been called before the cookie is deleted.

The code for deleting the cookie is quite simple:

jaaulde.utils.cookies.del('sessionID');

Thanks :)

+1  A: 

you can delete the cookie at the end of the process as well as in window.onUnload event to make sure that the cookie is cleared even if you are closing the window before the process completes.

Elangovan
+1  A: 
function serialEvent(eventNumber, eventName)
{
    var ok = false;
    try
    {
        var sessionID = jaaulde.utils.cookies.get('sessionID');             
        var remLength = 20 - eventName.length;  
        var remSession = sessionID.substr(sessionID.length - remLength, remLength);
        var eventName = eventName + remSession;
        s.events = eventNumber + eventName; 
        ok = true;
    }
    catch(e)
    {
        // todo: error handling (what has gone wrong?)
        ok = false;
    }

    return ok;
}

This way you can find out if the function is called correctly. ok will only be true if the whole function is executed correctly.

Snake
+1  A: 

There's no asynchronous or timer-delayed callback functions called in serialEvent function so you can either

  • Put it at the end of the function before the closing bracket, or
  • Put it after serialEvent('event21:', 'payment');.

Javascript executes synchronously, so you can be sure that the cookie is only deleted when you are finished with it.

Andy E