Giwrgos Gkogkas
Giwrgos Gkogkas

Reputation: 479

Detect if browser is performing a redirect

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 complete 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 complete.

Upvotes: 4

Views: 12174

Answers (1)

Giwrgos Gkogkas
Giwrgos Gkogkas

Reputation: 479

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.

Upvotes: 6

Related Questions