split()This is quite useful piece of JS to know. It does basically what it's called. It 'splits' a string into an array starting with 0, and going up. It might be difficult to explain, so I'll show you an example.
Take a look at this code. It's a typical and simple use of split():
<script type="text/javascript">
<!--
var x=location.href.split("?")[0];
document.write("Everything after the ? from this string will be split. Let's see what happens: "+x+"")
//-->
</script>
Alright, let's go through what has just happened up there. First as we always do with JavaScript is we set up our JavaScript tags, which are:
<script type='text/javascript'>
Code
</script>
Don't worry about the <!-- and the //--> right now.
Ok, we have now opened our JS tags and are ready to go.
The
white text is us declaring our new variable called x. You can name it whatever you like, I've just used x as an example.
The
orange text is us having our browser detect the url of the page we are currently viewing.
The
red text is our split() and the ("?") is where we are telling JS to split our detected url. The [ 0]; is our first array value. If we were to have [1] there, we would get everything after and including the ? in our url split.
The
blue text is us telling JS to write whatever is to follow the (" as text.
The
green text is just some regular text. I didn't include the yellow text with the green because of something I will tell you in a second.
The
yellow text is some more text but I want you to take note of the "+x+". That is us placing our variable that we had declared earlier on into our document.write so we will see what happens to our string. The reason it has it's own double quotes is because it is a variable, and the ++'s are there to add in whatever is in the variable (In this case you could also add the x variable outside of the quotes, whilst still in the brackets, with only one +. Since it's not by itself and is in the brackets of something, in this case the document.write, it doesn't need the double quotes because what's in the quotes for the document.write is all text. Therefore with a simple + and the variable name, we can append this onto our string in this scenario [thanks slayer766] ). So, it's kind of a shortcut when writing that specific url later on, and useful because if we wanted to have everyone be able to use the url, but they had different text before our split point, like our IF urls have (The server number and board name) it will grab their url, so it's automatic and they don't have to touch that portion of the script.
If we were to use this script, this is what the output would be:
Everything after the ? from this string will be split. Let's see what happens: http://phantom-designs.net/index.php
But what would be our output if we used [1] instead of our [ 0] value?
Everything after the ? from this string will be split. Let's see what happens: showtopic=14630
Now, that happened because we've grabbed the url of the page we're on, in this case this topic , and it has output everything after our split area. We are then left with
showtopic=14630There you are, an explanation on split(). If you have any questions that weren't covered in here, please post them and I'll be sure to add it to this post.
Enjoy!