Harshit Laddha
Harshit Laddha

Reputation: 2124

hard refresh of browser page and then redirect to a url using javascript

I am trying to reload the page and redirect it to another one, or just redirect it to another one but I can't do that by

window.location.href

or

windown.location

as I have an Angular Single Page app which catches the route and instead of reloading the page opens the partials directly

I did try -

window.location.reload(true)

but it reloads the page and cannot change the url I have also tried -

window.location.assign

and

window.location.replace

Is there any way to do this?

Upvotes: 2

Views: 17185

Answers (5)

CJ Dennis
CJ Dennis

Reputation: 4336

According to https://docs.angularjs.org/guide/$location, if AngularJS is using HTML5 mode, it never performs a full page reload in a modern browser.

HTML5 mode

In HTML5 mode, the $location service getters and setters interact with the browser URL address through the HTML5 history API. This allows for use of regular URL path and search segments, instead of their hashbang equivalents. If the HTML5 History API is not supported by a browser, the $location service will fall back to using the hashbang URLs automatically. This frees you from having to worry about whether the browser displaying your app supports the history API or not; the $location service transparently uses the best available option.

Opening a regular URL in a legacy browser -> redirects to a hashbang URL Opening hashbang URL in a modern browser -> rewrites to a regular URL Note that in this mode, Angular intercepts all links (subject to the "Html link rewriting" rules below) and updates the url in a way that never performs a full page reload.

(Emphasis mine)

Since Hashbang mode is the default, the code must be setting HTML5 mode somewhere.

Upvotes: 0

Reena
Reena

Reputation: 1119

There are three cases where AngularJS will perform a full page reload:

Links that contain target element

Example:

<a href="/ext/link?a=b" target="_self"> link  </a>

Absolute links that go to a different domain Example:

<a href="http://angularjs.org/">link</a>

Links starting with '/' that lead to a different base path when base is defined Example:

<a href="/not-my-base/link">link</a>

Updated:

Using javascript:

The $location service allows you to change only the URL; it does not allow you to reload the page. When you need to change the URL and reload the page or navigate to a different page, please use a lower level API: $window.location.href.

See:

https://docs.angularjs.org/guide/$location

https://docs.angularjs.org/api/ng/service/$location

Upvotes: 5

Collin Grady
Collin Grady

Reputation: 2243

So you need to change the hash on the end of the URL? i.e. from #/ to #/otherpage/ ?

If so, location.hash = "/otherpage/";

Upvotes: 0

Pankaj Parkar
Pankaj Parkar

Reputation: 136144

For redirect to different page you should use

$window.open('url', '_self')

Which will load your page again.

Upvotes: 2

Adarsh Hegde
Adarsh Hegde

Reputation: 631

try window.location = url;

do remember that the url must have a protocol i.e http:// or https://

Upvotes: 0

Related Questions