How to get request uri from location.href in javascript?

Viewed 53162

What I get from location.href is like this:

http://stackoverflow.com/questions/ask

But I only want to get questions/ask (no / at the first character)

How to achieve this?

5 Answers
var uri = window.location.href.substr(window.location.protocol.length + window.location.hostname.length + 2);

This code also includes GET and HASHTAGS (basically everything after hostname)

If you need the query params you can use:

var path = (window.location.pathname+window.location.search).substr(1);

If you need access to the hash as well you can use:

var path = (window.location.href.replace(window.location.origin, '')).substr(1);
Related