28

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)?

apxcode
  • 7,696
  • 7
  • 30
  • 41
Jer
  • 5,468
  • 8
  • 34
  • 41
  • 2
    When I did this, I had to construct it myself. I would be pleased if that weren't necessary. – Seiyria May 06 '14 at 22:28
  • Isn't $location.absUrl() returning what you want or why aren't you just stripping off the ? at the end? – Michael J. Calkins May 06 '14 at 22:32
  • $location.absUrl() has the query parameters ("?color=yellow&animal=rat") on the end. Easy enough to strip off but I was just wondering if there was a built-in function since they seem to have functions for just about everything else. – Jer May 06 '14 at 22:35
  • 6
    Are you opposed to using $window service? $window.location.origin + $window.location.pathname should get you what you want. – Mike Pugh May 06 '14 at 23:08
  • It seems the only way is to construct it by yourself. – wcc526 Mar 09 '15 at 07:34
  • As of the date of this comment, the mozilla developer docs state location.origin is "an experimental API and should not be used in production code" – ThinkingInBits Sep 29 '15 at 03:11
  • also try $location.$$path and $location.$$url – rahul rathore Feb 18 '17 at 17:21

2 Answers2

43

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]
Jamy
  • 901
  • 8
  • 12
  • 2
    also try $location.$$path and $location.$$url – rahul rathore Feb 18 '17 at 17:21
  • @Jamy How do I get this http://localhost/mac/app/pages/company2?companyId=1286 , right now I am just getting http://localhost/mac/app/pages/company2 – Sikandar Sahab Mar 15 '18 at 13:05
  • 2
    @BadshahTracker try this: `var url = $location.absUrl();` – Jamy Mar 18 '18 at 10:14
  • https://docs.angularjs.org/api/ng/service/$location - The `$location` service parses the URL in the browser address bar (based on the `window.location`) and makes the URL available to your application. Changes to the URL in the address bar are reflected into $location service and changes to `$location` are reflected into the browser address bar. – Jamy Nov 19 '21 at 10:38
9

Not that this eliminates the need to construct it by yourself, just another way to do the same. If you use window.location object, you can just say window.location.origin+window.location.pathname

window.location object has

host:"localhost.abc.com:8080"
hostname:"localhost.abc.com"
href:"http://localhost.abc.com:8080/quickpick/repossessions/?displayStr=Repossessions&from=%2F&page=1"(whole url)

origin:"http://localhost.abc.com:8080"
pathname:"/quickpick/repossessions/"
port:"8080"
protocol:"http:"
chandu
  • 123
  • 1
  • 6