最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I reload once using window.location.reload? - Stack Overflow

programmeradmin0浏览0评论

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");
})

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");
})
Share Improve this question edited Jul 1, 2015 at 17:16 asked Jul 1, 2015 at 1:24 user4229392user4229392 3
  • show your code. what is invoking it? – Daniel A. White Commented Jul 1, 2015 at 1:26
  • what's on your /incidents view?? – Sudhansu Choudhary Commented Jul 1, 2015 at 1:29
  • When I do login and try to go to my Index, angular doesn't detect my cookies at the first load, I need to reload my page – user4229392 Commented Jul 1, 2015 at 1:29
Add a ment  | 

2 Answers 2

Reset to default 3

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

wwww.example./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./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");
}

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");
}
发布评论

评论列表(0)

  1. 暂无评论