1

Is it possible from angular or JavaScript to catch URL changes in browser's address bar?

If someone changes the URL in the browser address bar by hand, I want to catch this event.

cbass
  • 2,548
  • 2
  • 27
  • 39
batog
  • 13
  • 4

2 Answers2

0

You might also be able to catch it with:

$rootScope.$on('$routeChangeStart', function(params) { 
   //catch event
});

You can read more about it at angular docs and stackoverflow

Community
  • 1
  • 1
cbass
  • 2,548
  • 2
  • 27
  • 39
  • Hi @cbass, $scope.$on could not catch when url changed by manualy – batog Oct 21 '14 at 15:31
  • Well, I mean. If the user presses enter it's probably gonna cause the browser to reload; then also application to reinitialize which means that there is no event to catch. – cbass Oct 21 '14 at 15:37
  • You mean there is no way to catch this. – batog Oct 21 '14 at 15:50
  • Yes, if you modify the url and press enter the browser will reload the page. Unless you prevent this is some kind of way. – cbass Oct 21 '14 at 15:53
  • This doesn't answer the question. I have no idea why the OP accepted this. – Mark Amery May 15 '15 at 09:40
-1

You can detect changes in the hash (e.g. http://example.com/#foo) by creating an event handler like so:

window.addEventListener('hashchange', function(e) { 
    console.log('Hash has changed!'); 
});

Specifically to angular, you can use the $routechangestart event to catch changes in the URL (i.e. the route)

$rootScope.$on("$routeChangeStart", function (event, next, current) {
    // do something ...
}); 

Alternatively, you can define your routes via the routeProvider and execute specific controllers based on the route that is set.

sigint
  • 34
  • 3