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 6219383
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:46:16+00:00 2026-05-24T07:46:16+00:00

What is the correct way to pull out just the path from a URL

  • 0

What is the correct way to pull out just the path from a URL using JavaScript?

Example:
I have URL
http://www.somedomain.com/account/search?filter=a#top
but I would just like to get this portion
/account/search

I am using jQuery if there is anything there that can be leveraged.

  • 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-24T07:46:16+00:00Added an answer on May 24, 2026 at 7:46 am

    There is a property of the built-in window.location object that will provide that for the current window.

    // If URL is http://www.somedomain.com/account/search?filter=a#top
    
    window.location.pathname // /account/search
    
    // For reference:
    
    window.location.host     // www.somedomain.com (includes port if there is one)
    window.location.hostname // www.somedomain.com
    window.location.hash     // #top
    window.location.href     // http://www.somedomain.com/account/search?filter=a#top
    window.location.port     // (empty string)
    window.location.protocol // http:
    window.location.search   // ?filter=a  
    


    Update, use the same properties for any URL:

    It turns out that this schema is being standardized as an interface called URLUtils, and guess what? Both the existing window.location object and anchor elements implement the interface.

    So you can use the same properties above for any URL — just create an anchor with the URL and access the properties:

    var el = document.createElement('a');
    el.href = "http://www.somedomain.com/account/search?filter=a#top";
    
    el.host        // www.somedomain.com (includes port if there is one[1])
    el.hostname    // www.somedomain.com
    el.hash        // #top
    el.href        // http://www.somedomain.com/account/search?filter=a#top
    el.pathname    // /account/search
    el.port        // (port if there is one[1])
    el.protocol    // http:
    el.search      // ?filter=a
    

    [1]: Browser support for the properties that include port is not consistent, See: http://jessepollak.me/chrome-was-wrong-ie-was-right

    This works in the latest versions of Chrome and Firefox. I do not have versions of Internet Explorer to test, so please test yourself with the JSFiddle example.

    JSFiddle example

    There’s also a coming URL object that will offer this support for URLs themselves, without the anchor element. Looks like no stable browsers support it at this time, but it is said to be coming in Firefox 26. When you think you might have support for it, try it out here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the correct way to import a C++ class from a DLL? We're
What is the correct way to do this? For example, how would I change
I'm using the garb gem to pull some basic stats, like pageviews, from Google
What is the correct way to check if a value is a date/number in
Is there a correct way in Windows Forms to flash a titlebar without having
What is the correct way to convert ASP.NET SOAP-based web services to JSON-based responses?
What is the 'correct' way to store a native pointer inside a Java object?
What is the correct way of retrieving maximum values of all columns in a
What is the correct way of iterating over a vector in C++? Consider these
What is the correct way to work with SQLite in WPF and ASP.NET MVC?

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.