Home > DeveloperSection > Forums > JavaScript - Get Portion of URL Path
Royce Roy
Royce Roy

Total Post:134

Points:938
Posted on    March-12-2013 3:35 AM

 JavaScript JavaScript 
Ratings:


 1 Reply(s)
 1368  View(s)
Rate this:

Hi Everyone!

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.

Thanks in advance



AVADHESH PATEL

Total Post:604

Points:4228
Posted on    March-12-2013 8:47 AM

Hi Royce!

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

// 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


Don't want to miss updates? Please click the below button!

Follow MindStick