Updating URL without redirection is not working

Viewed 165

I have tried window.history.pushState('', '', site_url + '' + ActivityUrl); and also window.history.replaceState('', '', site_url + '' + ActivityUrl);

I need to update the URL in the browser without redirecting to it. All the solutions I had got are the above two, but this is not working and also not displaying any errors.

The code I'm using is in an AngularJS controller,

$scope.updateUrl = function (ActivityUrl) {

    window.history.pushState('', '', site_url + '' + ActivityUrl);
}

and calling this function with a ng-click.

1 Answers

At first, you need to set the configuration for the location provider:

// Configure $locationProvider html5Mode
app.config(['$locationProvider', function($locationProvider) {
    $locationProvider.html5Mode({ enabled: true, requireBase: false, rewriteLinks: false });
}]);

And then change your method as:

$scope.updateUrl = function (ActivityUrl) {
    $window.history.pushState('', '', site_url + '' + ActivityUrl);
}
Related