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

16,687

Solution 1

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

Solution 2

For redirect to different page you should use

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

Which will load your page again.

Share:
16,687

Related videos on Youtube

Harshit Laddha
Author by

Harshit Laddha

I m a Web, Mobile &amp; Cloud Developer based out of Bangalore, India. I love to code &amp; develop awesome websites, apps.I am a generalist programmer with experience in multiple technologies and platforms. I have a flair for well-structured, readable, and maintainable applications and excellent knowledge of HTML, CSS, JavaScript, Python, PHP, Node.Js, SQL, MongoDB, Android (Java, Kotlin), iOS (Swift), React, Docker, AWS, GCP, etc.

Updated on June 04, 2022

Comments

  • Harshit Laddha
    Harshit Laddha almost 2 years

    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?

    • Pankaj Parkar
      Pankaj Parkar about 9 years
      you could try window.open('url', '_self')
    • deostroll
      deostroll about 9 years
      are you able to replicate this in a fiddle or plunk? plnkr.co/edit/5B6hH1Ro7CS4PmTDXTy4?p=preview ...you have to view it in a popped out window though...
    • Harshit Laddha
      Harshit Laddha about 9 years
      I just achieved it by hit and trial. what I did was after changing the location by $window.location.href = url; which did not work for me previously and now also when used alone. I added the window.location.reload(true) in the next line so that the window is reloaded with the new url instead
  • Harshit Laddha
    Harshit Laddha about 9 years
    No, any url changes they are captured by angular and it does not let the entire page refresh, just changes the view and controller. thats why window.location.reload(true) works for me and no other solution among the ones that I showed above but I want to change the url to in addition to the url change
  • Harshit Laddha
    Harshit Laddha about 9 years
    as I said in the question the window.location does not work and yes I did add http://
  • Adarsh Hegde
    Adarsh Hegde about 9 years
  • Harshit Laddha
    Harshit Laddha about 9 years
    It is not working, I have also tried var win = window.self win.open(url,"_self")
  • Harshit Laddha
    Harshit Laddha about 9 years
    I am calling this in a child scope does that have to do with anything?
  • Pankaj Parkar
    Pankaj Parkar about 9 years
    @deadman are you using modal window? why you don't think about bootstrap popup angular-ui.github.io/bootstrap
  • Pankaj Parkar
    Pankaj Parkar about 9 years
    this three ways are not angularjs specific, this is what about any html page :p