user4229392
user4229392

Reputation:

How can I reload once using window.location.reload?

I'm using AngularJs for my web application and I got problems with my login, the only solution that I found is reloading the page after login, but I need only once, but now is an infinite loop. How can I use Window.location.reload() only once

Login Method:

.success(function (data) {

        $scope.tokengenerated = data.token;

        $cookies.put('Username', UsernameValue);

        $cookies.put('Token', $scope.tokengenerated);

        $location.path("/incidents");
})

Upvotes: 2

Views: 5868

Answers (2)

Lucio Paiva
Lucio Paiva

Reputation: 20816

When loading the page for the first time, do it like this:

wwww.example.com/incidents?reload=true

Then check if the parameter is there:

if (location.search.indexOf("reload=true") != -1) {
    // refresh the page, but no "reload" this time
    location.href = "www.example.com/incidents";
}

Or using Angular.js:

if ($location.search().reload === "true") {
    // refresh the page, but no "reload" this time
    $location.path("/incidents");
}

Alternatively, you could check for the existence of the cookie you set just before reloading the page. If the cookie is there, do not reload; if it isn't, set it and then reload the page:

if (!$cookies.get("Token")) {
    // set cookies, do whatever you need here
    
    // reload it only once
    $location.path("/incidents");
}

Upvotes: 3

Try this... It's work!

var refresh = $window.localStorage.getItem('refresh');
console.log(refresh);
if (refresh===null){
    window.location.reload();
    $window.localStorage.setItem('refresh', "1");
}

Upvotes: 2

Related Questions