0

As there is a directive in AngularJs - onLocationChange() and C# has onActionExecute(), is there any event like these in Javascript which executes just before any action within the entire website.

Bünyamin Sarıgül
  • 3,031
  • 4
  • 30
  • 55
vikash5472
  • 200
  • 2
  • 16
  • What do you mean by "any action"? – Joseph Mar 29 '17 at 13:23
  • @JosephtheDreamer : I want it to excute before any ajax and location change. – vikash5472 Mar 29 '17 at 13:24
  • @VikashVerma - This has your answer http://stackoverflow.com/questions/11177233/javascript-event-that-runs-before-page-changes – Sudipta Mondal Mar 29 '17 at 13:26
  • 2
    You would need to write your own for before an Ajax call. Nothing like that exists out of the box. But if you don't want to write your own jQuery supports events that kick off before an Ajax call. As for when location changes I think I have read `beforeunload` is very unreliable. – AtheistP3ace Mar 29 '17 at 13:27
  • @AtheistP3ace : Is not any common event? – vikash5472 Mar 29 '17 at 13:33
  • Like a `onUserThought` event, or perhaps `onPrecog`? – isherwood Mar 29 '17 at 13:34
  • something like that.....but action instead of thought. – vikash5472 Mar 29 '17 at 13:37
  • @VikashVerma I am not sure I understand the question. There is no common event for before an Ajax call unless you are using jQuery. Then you would use `ajaxStart`. As for a common event to use before you move to a new page it is `beforeunload` but I have read that is not reliable since you can't guarantee it will be called or the code will complete before the page unloads. – AtheistP3ace Mar 29 '17 at 13:39

1 Answers1

0

from your question it seems like you want to observe user navigation i.e: when ever address bar location gets changed.

You can use an older method detecting whenever page gets refresh

window.addEventListener('beforeunload', function (e) {
    // perform  something here
}, false);

for SPAs (Single page applications you can use hashchange

//detecting hashchange
window.onhashchange = function() { 
     //code  
}
ieatbytes
  • 516
  • 1
  • 6
  • 13