Is there a built-in way to get the current URL without any query parameters?

Jer picture Jer · May 7, 2014 · Viewed 63.2k times · Source

If my URL is http://www.something.com/foo/bar/index.html?color=yellow&animal=rat, it seems as though:

  • $location.path() will return foo/bar/index.html
  • $location.absUrl() will return http://www.something.com/foo/bar/index.html?color=yellow&animal=rat
  • $location.url() will return foo/bar/index.html?color=yellow&animal=rat

Is there any function which will return http://www.something.com/foo/bar/index.html?

Or do I have to construct that myself with functions like protcol, host, port, etc. (or strip the query params off myself)?

Answer

Jamy picture Jamy · Nov 17, 2015

As far as I'm aware you have to construct it yourself. Not that you were asking how to construct it, but for those who are wondering:

var url = $location.absUrl().split('?')[0]