Closed Thread Icon

Topic awaiting preservation: Desperate to know split method, please? Pages that link to <a href="https://ozoneasylum.com/backlink?for=8769" title="Pages that link to Topic awaiting preservation: Desperate to know split method, please?" rel="nofollow" >Topic awaiting preservation: Desperate to know split method, please?\

 
Author Thread
Hiroki
Paranoid (IV) Inmate

From: NZ
Insane since: Dec 2002

posted posted 07-23-2003 01:36

Hi, guys. How are you?
Well, could I ask you about split method, please?
Please see this code:

code:
userName = ""
if(document.cookie != ""){
userName = document.cookie.split("=")[1]
}



And here is quote from my book:

quote:
The method split("=") splits a cookie record into field, where cookieField[0] is the cookie name and cookieField[1] is the cookie value. Note that cookieField can be anything that you want ot name a particular cookie's fields. So you assign userName the value returned by document.cookie.split("=")[1], that is, the cookie value.



Well, I have been confused what this means. Would you know what it means?
Sounds like this is talking about sort of cookie array??? Hmm...Please help!

Hiroki Kozai

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 07-23-2003 02:00

Split is a convenient function which takes a string and creates an array from it. It's best understood with an example:

Take this string:

var mystring = "Hello, my name is Slime";

and run split on it, splitting it on spaces (" "):

var myarray = mystring.split(" ");

myarray now contains the following values:

["Hello,", "my", "name", "is", "Slime"]

Basically, the split function literally splits the string every time it finds what you specified (in this case, the space character), and makes an array of everything inbetween.

So, now, let's assume that you have the string "name=value", and you run split("=") on it. You get back the array ["name", "value"]. This makes it easy to get the name and value of a cookie. You can get the value by doing this:

var nameandvalue = document.cookie.split("=");
var value = nameandvalue[1];

Or you can skip the inbetween step and just do it all at once, like this:

var value = document.cookie.split("=");

Make sense?

-----------------

OK, having explained that, I'd like to warn you that that code probably won't work. document.cookie contains a lot more than just a single "name=value" pair. Rather, it usually contains multiple pairs, one for each cookie which has been saved. I think they're separated by semicolons, like "name=value; name2=value2; name3=value3".

For more information on cookies, check out the FAQ, I think there's a bunch of info in there on them.

Hiroki
Paranoid (IV) Inmate

From: NZ
Insane since: Dec 2002

posted posted 07-23-2003 02:24

Hi, Slime.
Thank you for telling me that!
Great! I got it!
How are you today? I am just off to lunch now.
I have rice with some boiled veges.
I eat rice three times a day.
How about you?
Have a good day.
Cya.

Hiroki Kozai

« BackwardsOnwards »

Show Forum Drop Down Menu