Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Var: Get URL

  1. #1
    Join Date
    Jul 2009
    Location
    Washington (USA)
    Posts
    94
    Thanks
    3
    Thanked 3 Times in 3 Posts

    Question Var: Get URL

    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.
    Last edited by kaos; 09-20-2009 at 05:07 PM.

  2. #2
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Is it possible you could use PHP? Or is Javascript a must?
    Jeremy | jfein.net

  3. #3
    Join Date
    Feb 2009
    Posts
    23
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    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)

  4. #4
    Join Date
    Jul 2009
    Location
    Washington (USA)
    Posts
    94
    Thanks
    3
    Thanked 3 Times in 3 Posts

    Default

    @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?
    Last edited by kaos; 09-17-2009 at 11:06 PM.

  5. #5
    Join Date
    Jul 2009
    Location
    Washington (USA)
    Posts
    94
    Thanks
    3
    Thanked 3 Times in 3 Posts

    Default

    I just tried it, and it said 'undefined'.

  6. #6
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Try:
    item[1]
    Jeremy | jfein.net

  7. #7
    Join Date
    Feb 2009
    Posts
    23
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    I did the test yesterday and it worked...

    this was my test:
    Code:
    <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:
    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>
    Last edited by NDK; 09-18-2009 at 07:01 AM.

  8. #8
    Join Date
    Jul 2009
    Location
    Washington (USA)
    Posts
    94
    Thanks
    3
    Thanked 3 Times in 3 Posts

    Default

    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.

  9. #9
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    There you go.

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

  10. #10
    Join Date
    Jul 2009
    Location
    Washington (USA)
    Posts
    94
    Thanks
    3
    Thanked 3 Times in 3 Posts

    Default

    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?

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •