Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating URL without redirection is not working

Tags:

javascript

url

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.

like image 274
Saket Avatar asked Aug 31 '18 05:08

Saket


People also ask

How to change URL without reloading the page?

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.

How to change URL without reloading page in jquery?

history. pushState({page: "another"}, "another page", "example. html"); This will change the URL, but not reload the page.

How to change URL using JavaScript?

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.

How do I change my current URL?

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.


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);
}
like image 83
Sapna Bhayal Avatar answered Oct 04 '22 19:10

Sapna Bhayal