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

javascript - Detect if browser is performing a redirect - Stack Overflow

programmeradmin0浏览0评论

On my project I have a warning message that apears on every case of error.
It is not anything fancy, just some jquery transitions when a event is fired.
I have some pages that on load fetch some data with get requests.
When I do a redirect or a reload the connection with the server fails and triggers the warning to appear.
I believe that this behavior will confuse the users.

Is there a way to detect when a browser is performing a redirect or a reload in order to disable the warning?

Edit 1
I get an error because the browser is dropping the connection with server due to the redirect/reload.
So it is logical to get an error in my console.
The illogical thing is to throwgh a warning for something that the browser had to do in order to plete my request. I don't need an event, I need a way to check the browsers condition.

Edit 2
I should have said this earlier, but the warning is shown before the redirect or reload is plete.

On my project I have a warning message that apears on every case of error.
It is not anything fancy, just some jquery transitions when a event is fired.
I have some pages that on load fetch some data with get requests.
When I do a redirect or a reload the connection with the server fails and triggers the warning to appear.
I believe that this behavior will confuse the users.

Is there a way to detect when a browser is performing a redirect or a reload in order to disable the warning?

Edit 1
I get an error because the browser is dropping the connection with server due to the redirect/reload.
So it is logical to get an error in my console.
The illogical thing is to throwgh a warning for something that the browser had to do in order to plete my request. I don't need an event, I need a way to check the browsers condition.

Edit 2
I should have said this earlier, but the warning is shown before the redirect or reload is plete.

Share Improve this question edited Sep 2, 2015 at 12:26 Giwrgos Gkogkas asked Sep 2, 2015 at 11:48 Giwrgos GkogkasGiwrgos Gkogkas 4791 gold badge10 silver badges24 bronze badges 6
  • I'd personally be more concerned about the server error, but in answer to your question there is no "afterredirect" event, or similar. You could set a cookie before redirect, and don't show the error if that cookie value exists (clearing it every time you check, of course). – Reinstate Monica Cellio Commented Sep 2, 2015 at 11:52
  • @Archer could you check my update? – Giwrgos Gkogkas Commented Sep 2, 2015 at 11:58
  • How is a reload any different to the first visit to the page? I don't get why there should be a server error at all and I think you'd be spending your time better fixing that issue, than hiding it with something else. My cookies suggestion will still do the "something else", after your question update. – Reinstate Monica Cellio Commented Sep 2, 2015 at 12:00
  • @Archer When you leave a page with active connections the browser cuts the connection. This is correct since you are leaving the page and the connection is no longer needed. In reality there is no error, javascript handles it as an error. I can't stop the browser from closing a connection when I give him the instruction to drop everything that he is doing to perform a redirection. – Giwrgos Gkogkas Commented Sep 2, 2015 at 12:11
  • Closing a connection should not force an error when the next page loads, which is what your question sounds like you're saying. Are you now saying that you get the error before redirection or reload? – Reinstate Monica Cellio Commented Sep 2, 2015 at 12:18
 |  Show 1 more ment

1 Answer 1

Reset to default 6

Since I couldn't find a way to detect the browsers state I decided to go with jquery beforeunload event which I tried to avoid.
Using this event the solution is simple.

Step 1 Create a control var set to true;

example = true;

Step 2 Bind the before unload event to invert the var.

$(window).bind('beforeunload', function(){
    example  = false;
});

Step 3 Check your var before you execute your code.

if(example){
  controlled code...
}

The code will execute will the browser stands still but will be disabled if a redirect or reload is executed.

发布评论

评论列表(0)

  1. 暂无评论