SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict AHMED's Avatar
    Join Date
    Oct 2002
    Location
    Egypt
    Posts
    399
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    split using java script

    hi all

    i have a string which is the url of the page ..some thing like this http://www.sitepoint.com/forums/forumdisplay.php?f=15

    no i need to split this string using the "/" to know the page name like this forumdisplay.php?f=15 and assign it to anew variable ..


    so can any body help ?

  2. #2
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Code:
    function getFileName(url) 
    {
      //split URL string
      var filesArr = url.split('/');
      //assign last array member to filename
      var filename = filesArr[filesArr.length];
      return filename;
    }
    One question though: What happens with a URL like:
    That won't really be too helpful in that instance, since the querystring has slashes in it.

  3. #3
    SitePoint Addict AHMED's Avatar
    Join Date
    Oct 2002
    Location
    Egypt
    Posts
    399
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi vinnie
    thx 4 response
    i tried to use it like this

  4. #4
    SitePoint Addict AHMED's Avatar
    Join Date
    Oct 2002
    Location
    Egypt
    Posts
    399
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi vinnie

    sorry 4 last post it was amistake

    ===============

    tried to use it like this

    Code:
     
    <script>
    var Turl=parent.location.href;
    var filesArr = Turl.split('/');
    var filename = filesArr[filesArr.length];
    Durl="contact.asp";
    document.write (filename);
    </script>
    and the output of it comes "undefined" .. i mean it writes "undefined" in the page istedd of the value of 'filename' variable ..

  5. #5
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    var oRegExp = /([^\/]*\??[^\?]*)$/;
    var Turl = parent.location.href;
    var Durl='contact.asp';
    if (typeof Turl.match(oRegExp)[1] != 'undefined')
        document.write(Turl.match(oRegExp)[1]);
    ::: certified wild guess :::

  6. #6
    SitePoint Wizard Defender1's Avatar
    Join Date
    Apr 2001
    Location
    My Computer
    Posts
    2,808
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by AHMED
    sorry 4 last post it was amistake
    That's what the Edit post button is for.
    Defender's Designs
    I'm Getting Married!

    Not-so-patiently awaiting Harry Potter Book 7 *sigh*


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
  •