05 July, 2008, 07:08:36 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: We Return!
 

Pages: [1]   Go Down
  Print  
Author Topic: split()  (Read 281 times)
0 Members and 1 Guest are viewing this topic.
rawr


Bronze Contribution
*
Elite Member

Karma: 8
Offline Offline

Gender: Male
Posts: 150
2995.50 Secksi Coins

View Inventory
Send Money to Vitality

View Profile
« on: 26 April, 2008, 04:08:04 AM »

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():
Quote
<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:
Code:
<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:
Code:
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?
Code:
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=14630

There 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!
Logged

Hey
This resource was found useful by 0 out of 0 members. Rate it: [applaud] [smite]
United States
Long live TCZ

TCZ r0x my s0x

Administrator

Platinum Contribution
*
Conqueror

Karma: 70
Offline Offline

Gender: Male
Posts: 4689
679832.36 Secksi Coins

View Inventory
Send Money to slayer766

View Profile
« Reply #1 on: 26 April, 2008, 04:14:50 AM »
Take me down to the paradise city where the grass is green and the girls are pretty!

Great tutorial Vitality, will you be writing up a tutorial on join() as well?
Logged

Live for nothing or die for something




rawr


Bronze Contribution
*
Elite Member

Karma: 8
Offline Offline

Gender: Male
Posts: 150
2995.50 Secksi Coins

View Inventory
Send Money to Vitality

View Profile
« Reply #2 on: 26 April, 2008, 05:06:09 AM »

I'm not too familiar with it, but I could look into it tonight and see what I can come up with. Never hurts to learn something new anyway.
Logged

Hey
Pages: [1]   Go Up
  Print  
 
Jump to:  
Sponsored Links:

Powered by MySQL
Powered by PHP
Powered by SMF 1.1.1 | SMF © 2005, Simple Machines LLC
TinyPortal v0.8.6 © Bloc
The rest copyright TCZ you pillocking twat!
Valid XHTML 1.0!
Valid CSS!
Places we'd like you to visit.