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.
Method 2: Adding a new state with pushState() Method: The pushState() method is used to add a new history entry with the properties passed as parameters. This will change the current URL to the new state given without reloading the page.
history. pushState({page: "another"}, "another page", "example. html"); This will change the URL, but not reload the page.
href = "https://www.example.com"; // Assigns a new URL to the current window. window. location. assign("https://www.example.com"); // Replaces the location of the current window with the new one.
Answer: Use the window. location. href Property location. href property to get the entire URL of the current page which includes host name, query string, fragment identifier, etc.
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With