PDA

View Full Version : Resolved Var: Get URL



kaos
09-17-2009, 02:39 AM
Hello Everyone!

I have a kinda complicated problem. I need a way to call part of the URL of a page (in a var). For example, if the URL was 'http://www.mydomain.com/video.html?item=3' i would like to call only the '3' part. However, (the complicated part) the number at the end could be several digits (they're all different). Does anyone know how to do this? Feel free to ask questions.

Nile
09-17-2009, 03:36 AM
Is it possible you could use PHP? Or is Javascript a must?

NDK
09-17-2009, 09:49 AM
can't you just use the split method in javascript?
var item = url.split("item=");

item is now an array and then you can use item[1] (this is 3)

kaos
09-17-2009, 10:49 PM
@Nile

Sorry, it has to be JavaScript.
------------------------------------------------------------
@NDK

So, your saying i could place 'var item = url.split("item=");'. Then, I could call it with 'item' where i need it, right?

kaos
09-17-2009, 11:18 PM
I just tried it, and it said 'undefined'.

Nile
09-17-2009, 11:21 PM
Try:
item[1]

NDK
09-18-2009, 06:54 AM
I did the test yesterday and it worked...

this was my test:

<script type="text/javascript">
var url = "http://www.mydomain.com/video.html?item=3"
var item = url.split("item=");
document.write(item[1]);
</script>
result = 3

item is the array, item[0] is everything before item=, item[1] is everything after item=

So if you have an url like "http://www.mydomain.com/video.html?item=3&node=5", you'll need to use a second split to have only 3 as result
code:

<script type="text/javascript">
var url = "http://www.mydomain.com/video.html?item=3&node=5"
var item = url.split("item=");
var newItem = item[1].split("&");
document.write(newItem[0]);
</script>

kaos
09-18-2009, 02:12 PM
That's great and all, really it is. however, there's still a little problem. if i write 'var url = "http://www.mydomain.com/video.html?item=3"', it will always write 3, even if the item really is 7. I want the script to call it from the acually url.

Schmoopy
09-18-2009, 03:25 PM
There you go.


<script type="text/javascript">
var url = document.location.href;
var item = url.split("item=");
document.write(item[1]);
</script>

kaos
09-18-2009, 10:15 PM
thank you so much. however, i still have one more problem. when i call it, it places a comma before the the number. why is that?

kaos
09-19-2009, 08:14 PM
Never mind... I got it! Thank you all so much!

Nile
10-01-2009, 11:51 AM
Glad to help.
It seems your topic is solved... Please set the status to resolved.. To do this:
Go to your first post ->
Edit your first post ->
Click "Go Advanced" ->
Then in the drop down next to the title, select "RESOLVED"