0

Target: Changing the URL without reloading the page because the URL is used for later functionalities but the relinks from other locations can't be changed easily.

Enviroment: ServiceNow instance (that's why the syntax may look a bit weird to you guys at some points.

I need to reactive the event handling for the $locationChangeStart after preventing it once.

Any idea how to revert the e.preventDefault() functionality?

Code:

var originalUrl, initUrl;
$rootScope.$on("$locationChangeStart", function(e, newUrl){
            
if(newUrl != originalUrl && newUrl != initUrl && c.count<c.maxCount ){
   $window.history.replaceState(newUrl, $document.title, newUrl);
   c.count++;
}

   originalUrl = newUrl;
   e.preventDefault();
   $timeout(function(){
      console.log("unbind")
      //Both not working
      $().unbind(preventDefault);
      e.returnValue = true;
   },2000)
});

$scope.changeURL = function(){
   initUrl = $location.absUrl();      
   var params = $location.search();
   params.o=$scope.data.sort_fields;
   params.d=$scope.data.sort_direction;
   params.initial_load= true;

   //triggers the $locationChangeStart event
   $location.search($httpParamSerializer(params));
}
$scope.changeURL()
bad_coder
  • 11,289
  • 20
  • 44
  • 72

1 Answers1

0

I didn't know that e.preventDefault() has to be called constantly to stop events. So there is no need to revert it. It is only required to stop calling e.preventDefault() if it shouldn't be called.