PDA

View Full Version : Retrieving cookies through JS, inserting into form


Charles Capps
04-06-1999, 10:05 PM
Ok, I've searched the 'net a dozen times for a script that can do it, but can't find any that work.

Right now I'm resisting the urge to rip the code right out of the UBB... I want something I can actually include when I distribute my script. :-)

If it weren't for the fact that this particular form MUST be static (html) rather than dynamic like the rest of the site, this wouldn't be an issue. **SIGH**

Any ideas on this?

------------------
"Okay, so I'm not "SANE" so to speak, but uh... I'm the lovable kind of psycho"
http://solareclipse.net/

Charles Capps
04-06-1999, 10:50 PM
Aha, found a solution...
<script language="Javascript">
<!-- Hide

function WM_readCookie(name) {
if(document.cookie == '') return false;
else return unescape(WM_getCookieValue(name));
}

function WM_getCookieValue(name) {
var firstChar, lastChar, endOfName;
var theBigCookie = document.cookie;
firstChar = theBigCookie.indexOf(name);
if((firstChar != -1) && (theBigCookie.charAt(firstChar + name.length) == '=')) {
firstChar += name.length + 1;
lastChar = theBigCookie.indexOf(';', firstChar);
if(lastChar == -1) lastChar = theBigCookie.length;
return theBigCookie.substring(firstChar, lastChar);
} else {
return false;
}
}

document.REPLY.name.value = WM_readCookie('UserName');
document.REPLY.email.value = WM_readCookie('Email');

// --> </script>

Courtesy Webmonkey in part. The REPLY in the last two lines correponds to the name of the form. The problem now is that it is NOT unescaped - spaces are still plusses, for instance... HELP! http://www.aota.net/ubb/smile.gif

------------------
"Okay, so I'm not "SANE" so to speak, but uh... I'm the lovable kind of psycho"
http://solareclipse.net/

Justin
04-06-1999, 10:52 PM
Here's a hint: Click "Reply" and view --> source http://www.aota.net/ubb/smile.gif The UBB does 99% of it's cookie work through JavaScript - so when it fills in your name and password, that's how it's done. Scroll toward the bottom of the source and you'll see it.

Hope this helps http://www.aota.net/ubb/biggrin.gif

<edit>
Man you're quick!
</edit>

------------------
Justin Nelson
FutureQuest Support




[This message has been edited by Justin (edited 04-06-99).]

Charles Capps
04-06-1999, 11:08 PM
Ya, but there are two things...
1) I'm working on something in which I don't want to use code that comes from the UBB, you'll find out why later
2) I DID try looking at the UBB's implementation of it, but all it does is a simple unescape, which is obviously NOT swapping the + for a space. :-)

I'm a total JS novice...

------------------
"Okay, so I'm not "SANE" so to speak, but uh... I'm the lovable kind of psycho"
http://solareclipse.net/

*edit* And 99% is not quite accurate - all cookie setting is done in JS, while about half of the reading is done in it. The other half is done in perl, so that means only 75% of cookie stuff is JS... But I nitpick/digress. :-)

[This message has been edited by Charles Capps (edited 04-06-99).]

Del
04-06-1999, 11:40 PM
spaces are still plusses, for instance... HELP!


Have a look at www.downinit.com/stuff/parse.html (http://www.downinit.com/stuff/parse.html) and check whatever it was I did to change the +'s into spaces. I can't remember it right now, and if I tried I'd forget the stuff I need to remember... hehehe

Del

Charles Capps
04-07-1999, 12:47 AM
ARGH! *bangs head against wall*

Now I remember why I started learning perl instead of Javascript.

I could LEARN perl.

I have moved the form onto its own CGI-generated page. Easier to do the cookie thing that way.

------------------
"Okay, so I'm not "SANE" so to speak, but uh... I'm the lovable kind of psycho"
http://solareclipse.net/

Justin
04-07-1999, 01:30 AM
hehe - I thought it was important that it be a static page? Or is it more important that it actually works? http://www.aota.net/ubb/smile.gif

I prefer Perl over JS anyday though - but if it must be static, have you thought about using php? Php has a few cookie functions built right in.

------------------
Justin Nelson
FutureQuest Support

Charles Capps
04-07-1999, 01:42 AM
Well, if PHP were on every single web host (I want to get the largest possible target audience), and I could test it here on my computer, then I'd use PHP in an instant. http://www.aota.net/ubb/biggrin.gif

I was just hoping that I could keep it on the static page, it would fit in better with the rest of the program. Oh well... At least now it's working. http://www.aota.net/ubb/smile.gif

------------------
"Okay, so I'm not "SANE" so to speak, but uh... I'm the lovable kind of psycho"
solareclipse.net/ (http://solareclipse.net/)

[This message has been edited by Charles Capps (edited 04-07-99).]