How to get only filename from the current page URL

Discussion in 'JavaScript' started by freelancerguy, Jan 7, 2009.

  1. #1
    freelancerguy, Jan 7, 2009 IP
  2. cont911

    cont911 Peon

    Messages:
    50
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    var url = 'http://www.web.com/abc/xyz/index.php?a=1&b=2';
    var lastSlash = url.lastIndexOf("/");
    var firstQuestionMark = url.indexOf("?");
    if(firstQuestionMark == -1)
    {
    firstQuestionMark = url.length;
    }

    var fileName = url.substring(lastSlash + 1, firstQuestionMark);
    alert(fileName);
     
    cont911, Jan 8, 2009 IP