• Hi all. We have had reports of member's signatures being edited to include malicious content. You can rest assured this wasn't done by staff and we can find no indication that the forums themselves have been compromised.

    However, remember to keep your passwords secure. If you use similar logins on multiple sites, people and even bots may be able to access your account.

    We always recommend using unique passwords and enable two-factor authentication if possible. Make sure you are secure.
  • Be sure to join the discussion on our discord at: Discord.gg/serebii
  • If you're still waiting for the e-mail, be sure to check your junk/spam e-mail folders

Javascript trouble!

Yo! First time writing javascript, though I've had a bit of experience with other languages. This function is mostly taken from code I've found on some of the tutorial sites, so it presumably works. It appeared to work when I tested it on the site I'm building but when further changes were made to the css and so forth, a nasty bunch of other strange things started happening.

I'm thinking it'd be a lot easier to fix the other problems if I actually UNDERSTOOD what this function was doing. The comments that came with it were rather non-explicit and perhaps intended for someone actually familiar with the javascript syntax. @_@ Help anyone?

function get_Cookie(name) {
var search =name + "="
var returnvalue ="";
if (document.cookie.length > 0) {
firsty = document.cookie.indexOf(search)
if (firsty!= -1) {
firsty+= search.length
lasty = document.cookie.indexOf(";", firsty);
if (lasty == -1)
lasty = document.cookie.length;
returnvalue=unescape(document.cookie.substring(firsty,lasty))
}
}
return returnvalue;
}


Now, I'm guessing that what it's actually DOING is to take the position in the cookie string of the start of the 'value' part and the position of the end of the value part and outputting the substring between them. But I'm just guessing here.

And what on earth does the '+=' mean in the line 'firsty+= search.length'? And is search a regular variable or one of those special words that javascript already knows about? (Please pardon the terminology slip there... can't remember what those things are called. Exception words? Bah, it doesn't matter anyway. You lot know what I'm talking about, right?)

Anyway, a step through of what this little beauty is doing would really help. Thanks!

Piney.
;204;;324;
 

serialcereal

Marsh Trainer
I've dabbled in script and to me it's a if and statement
function get_Cookie(name) {
var search =name + "="
var returnvalue ="";
if (document.cookie.length > 0) {
firsty = document.cookie.indexOf(search)search is an undifined variable
if (firsty!= -1) {
firsty+= search.length
lasty = document.cookie.indexOf(";", firsty);
if (lasty == -1)
lasty = document.cookie.length;
returnvalue=unescape(document.cookie.substring(fir sty,lasty))
}
}
return returnvalue;
}
what is it trying to do?
 
serialcereal said:
I've dabbled in script and to me it's a if and statement
function get_Cookie(name) {
var search =name + "="
var returnvalue ="";
if (document.cookie.length > 0) {
firsty = document.cookie.indexOf(search)search is an undifined variable
if (firsty!= -1) {
firsty+= search.length
lasty = document.cookie.indexOf(";", firsty);
if (lasty == -1)
lasty = document.cookie.length;
returnvalue=unescape(document.cookie.substring(fir sty,lasty))
}
}
return returnvalue;
}
what is it trying to do?

Essentially, it's reading the data out of a cookie. And it DOES work... but I'm not overly sure why or how. It's used in conjunction with the set cookie function
function stylecookie(stylesheetid) {
document.cookie = "stylesheetname=" + stylesheetid;
}

and once the info has been stored in the cookie (in this case, in the string "stylesheetname=*the value of stylesheetid*"), the first function I asked about can be used to retrieve what was passed to the set cookie function. Basically, stylecookie stories a value in a cookie and get_Cookie retrieves that value.

What I'm asking is that some kind person steps through the the get_Cookie function and puts some comments in there so I can understand what each line is doing. I'm getting the general gist of what it's doing, but I'd really like to understand it better.

Thanks heaps!

Piney.
 

Magma Leader Maxie

Non caedor caedo
Pinecone Tortoise said:
function get_Cookie(name) {
I suppose you understand what this does. It marks the start of a function called "get_Cookie" that receives one parameter, which will be reffered to as "name" within the function. So if you later call the function like this: get_Cookie('Maxie'), the variable "name" will have the value "Maxie" in the function. If you have a tiny bit of knowledge from any programming or scripting language, you should already know this.
var search =name + "="
Now a new variable is decalred, called "search". The variable "search" will have the value of the name supplied to the function, and an equals sign. So, if the name supplied is "Maxie", the variable search will have the value "Maxie=". Simple. And no, "search" isn't a built-in superglobal or anything, it's an invented variable.
var returnvalue ="";
A variable called "returnvalue" is declared. It has no value, therefore, we could say it's pretty much empty. Whoever wrote this script obviously had some programming habits from pascal or Java, where you must declare variables before you may use them.
if (document.cookie.length > 0) {
What follows is an if statement. The code that follows will be run if the cookie's length is more than 0, therefore, what follows will be done only if a cookie is set. If the person viewing the site doesn't have a cookie set by the site, this part will be skipped.
firsty = document.cookie.indexOf(search)
if (firsty!= -1) {
Hmm, the author of the script decided that the variable "firsty" will contain the position (expressed as a number) of the string contained in the variable "search". The "search" var contains "Maxie=", so the "firsty" var will search the cookie for the first occurence of the string "Maxie=". Let's say the cookie contains this: hsdaldjMaxie=dasdasiheal3. It probably won't contain this random garbage, but let's say it does. The variable "firsty" will now contain the number of letters before the first time "Maxie=" appears in the cookie. So, if the cookie was to contain what I coloured blue above, the variable "firsty" would contain "7". Get it?

If the variable "firsty" is equal to -1, it means that there is not a single "Maxie=" in the whole cookie. The next part does something if Maxie= was indeed found somewhere in the cookie, because then firsty wouldn't be equal to -1.
firsty+= search.length
All this does is add the length of the search variable to the variable "firsty". So if firsty is 7, we add 6 to it (because the length of "Maxie=" is six, count it yourself!). As simple as that. Now we end up with a variable called "firsty" which contains the number 13.
lasty = document.cookie.indexOf(";", firsty);
This stores something into the variable "lasty". But what? It stores the position of the ";" (semicolon) since the start of the cookie expressed as a number, but only after the first 13 characters! So if there is a semicolon before the first 13 characters, it will be ignored. (remember that firsty=13)
if (lasty == -1)
lasty = document.cookie.length;
If a semicolon wasn't found after Maxie=, then lasty will be equal to -1. If this is the case, the script now wants lasty to be equal to the length of the whole cookie.
returnvalue=unescape(document.cookie.substring(firsty,lasty))
}
}
return returnvalue;
}
"returnvalue" is assigned to the unescaped value of the substring-ed segment of the cookie. If you didn't understand that, here's a better explanation: firstly, the substring function is preformed on the cookie. The first 13 characters and the last 12 are removed from the cookie (why is it 12? because that's the number of characters after the semicolon in our example, but it could just as well be 56 or 97.) In effect this leaves you with whatever goes after "Maxie=" and before the semicolon. This value is then "unescaped", which in javascript terminology means that any hexadeciamal values will be convered to their ASCII equivalents.

After all that, the function returns the value "returnvalue".

Here's what the function really does: it returns whatever is located between name= and ;, where name is whatever you need. This is a nice little function for fetching values stored in a cookie.

If you had a cookie like this:
Name=John; Surname=Smith; Id=45; Age=30; HairColour=Brown;

Then is you did this:

Code:
var name = get_Cookie('Name');
var surname = get_Cookie('Surname');
var id = get_Cookie('Id');
var age = get_Cookie('Age');
var hair = get_Cookie('HairColour');

You would end up with the values stored in the cookie! name would be equal to "John", surname to "Smith", etc.

A nice and useful function. Hope I helped you a bit. Have fun!

P.S. I did a bit of excessive explaining here, but it's all for the best.
 
Last edited:
.... dude, that is SO helpful. Thankyou. Thankyou very very very very much. ^^ I'm so pleased about this. I hadn't realised just how much that function could do. Thanks heaps for your help!

Piney.
;204;;324;
 
Hey again!

Um, having a bit of trouble actually SETTING cookies that have semicolons in them. I've tried several different approaches, but it simply doesn't seem to work. Why, and how can I get around it?

Piney.
;204;;324;

EDIT: Nevermind, problem solved!
 
Last edited:
Top