Web Analytics Made Easy -
StatCounter Grabbing part of the url - CodingForum


No announcement yet.

Grabbing part of the url

  • Filter
  • Time
  • Show
Clear All
new posts

  • Grabbing part of the url

    Hello, does anyone here know how to use javascript to grab part of a url?

    For instance, lets say the current url is: http://www.codingforum.net/newthread...=newthread&f=2

    How do i use javascript to grab the "/newthread.php?do=newthread&f=2" part?

    Thanks in advance
    Last edited by ian_hawdon; Sep 30, 2006, 04:57 PM. Reason: Typo

  • #2
    Hi there!

    Hi there!

    I'm a total newb to JavaScript, so if I tried to go into too much depth on any of this, I'd probably end up talking nonesense.

    Here's a link to a few string functions that I've found useful in the past though; hopefully they might help you out, just in case nobody replies with anything better.



    • #3
      you could use javascript to split on | and then use the last array key.

      var string = 'http://domain.com/page.php?id=5&type=cheese';
      var pieces = string.split('/');
      That might do it for you, if it doesnt, it should lead you in the right direction.
      Active PHP/MySQL application developer available for immediate work.
      syosoft.com mavieo.com - Remote Web Site Administration Suite - Reseller Ready


      • #4
        If your URL is http://www.somedomain.com:80/story/index.html#aa?b=1 then the following will give you the various parts.

        location.hash = #aa
        location.host = www.somedomain.com
        location.hostname = somedomain.com (may still have the www. on the front)
        location.href = http://www.somedomain.com/story/index.html#aa?b=1
        location.pathname = story/index.html
        location.port = 80
        location.protocol = http:
        location.search = ?b=1

        as you can see, all of the different pieces are available in separate fields so you don't need to go splitting anything.
        Learn Modern JavaScript - http://javascriptexample.net/
        Helping others to solve their computer problem at http://www.felgall.com/

        Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


        • #5
          Thankyou, that got me what i wanted