View Full Version : pull variable from cookie filename

10-11-2011, 11:46 PM
I'm working on an internal website at work and employees have a cookie created when they login to the customer support tool. The filename is cookie:username@domain.com/ and I'd like to use javascript to pull a variable for their username to plug into a URL for a unique link when they're viewing one of our intranet pages.

I'm a Javascript newbie so any help is appreciated. @domain.com/ will always be the same but I don't know how to pull the unique username from the filename of the cookie. I don't need to actually read the cookie.


Logic Ali
10-12-2011, 01:49 AM
str = "cookie:username@domain.com/" ;

var uName = ( uName = str.match( /^cookie:([^@]+)@/ ) ) ? uName[ 1 ] : "" ;

alert( uName );

The above statements would extract the part of str betrween : and @. If I understand what you're doing, first you have to find a cookie name that matches the required format, and you must ensure that there is never more than one of those stored on any given browser or how would you distinguish them?

Old Pedant
10-12-2011, 04:00 AM
I give up. How can you even *READ* the cookie file names???

In general, the browser will *NOT* give you access to files stored on the user's computer. You can do this with MSIE, using the Scripting.FileSystemObject ActiveX control, but only if the user's MSIE browser settings allow it (and even then it has to be approved each time it is used).

Reading the cookie--without knowing or needing to know the filename--would be much the preferable way. It works cross-browser and requires no special permissions.