Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6234701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T10:32:05+00:00 2026-05-24T10:32:05+00:00

I wanna get url path by javascript, for example, i have these urls: http://localhost:8080/blah/blah/index.php?p=1

  • 0

I wanna get url path by javascript,
for example, i have these urls:

http://localhost:8080/blah/blah/index.php?p=1

http://localhost:8080/blah/blah/index.php

http://localhost:8080/blah/blah/

http://localhost:8080/blah/blah

and i would like to get: http://localhost:8080/blah/blah/
anyone please help me

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T10:32:07+00:00Added an answer on May 24, 2026 at 10:32 am

    This appears to be an ambigious problem with no definitive solution because there is no concrete definition of what you want removed from the end of the URL.

    It would be easy to remove the query parameters (first example).

    It would be easy to normalize the trailing slash (last two examples).

    It would be easy to remove the last piece of the path if that is what you always wanted, but unless you’re willing to add some more rules that can distinguish /index.php from /blah at the end of the URL so that an algorithm knows when to remove one, but not the other, what you have asked for is not possible. As you have defined the problem, the code cannot know whether
    the blah at the end of http://localhost:8080/blah/blah should be removed or not.

    For example, this code will remove the query parameters and the last piece of the URL, but that will only work on the three of the four examples, but runs into the ambiguity about what you want removed at the end:

    function cleanURL(url) {
        return(url.replace(/\?.*$/, "").replace(/\/[^\/]*$/, "") + "/");
    }
    

    And here it is being run on all four of your examples: http://jsfiddle.net/jfriend00/2Q4Ue/. It returns these results for the four cases:

    http://localhost:8080/blah/blah/
    http://localhost:8080/blah/blah/
    http://localhost:8080/blah/blah/
    http://localhost:8080/blah/
    

    For the last URL, you can see that the algorithm identified /blah at the end as the filename and removed it since there is no way to know if that’s part of the path or a filename.

    If you were willing to make a new rule that the filename must have a file extension and the last piece of the pathname when there is no filename will never have an extension, then such an algorithm could be written to handle the last case.

    OK, with a new rule that a filename at the end is anything with a period in it, then, you can use this:

    function cleanURL(url) {
        return(url.replace(/\?.*$/, "")
               .replace(/\/[^\/]*\.[^\/]*$/, "")
               .replace(/\/$/, "") + "/");
    }
    

    Which is shown in this fiddle: http://jsfiddle.net/jfriend00/LpauM/ and produces this result for the four test URLs:

    http://localhost:8080/blah/blah/
    http://localhost:8080/blah/blah/
    http://localhost:8080/blah/blah/
    http://localhost:8080/blah/blah/
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wanna use php to download some zip files from a url. I.E. sitename.com/path/to/file/id/123
Now I have a stack of free time on my hands, I wanna get
I wanna rewrite links like index.php?page=entry&id=15&action=edit to entry/15/edit . This is how my .htaccess
i've a jquery script which post/get data to .php script. but i wanna prevent
Okay, I am using (PHP) file_get_contents to read some websites, these sites have only
I wanna to get the query string of a page within a JavaScript block.
I have a tableView where the user can add contacts to. These contacts get
I wanna get the Timedate value from another page using request.querystring and then use
I want to get print out invoice but also wanna set margins and page
I wanna add server controls by using javascript. The main purpose of why I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.