2

window.onbeforeunload=function(); is not working in chrome. It works for IE and Mozila but it not for chrome. Is there any another way to use window.onbeforeunload=function(); in chrome?

Please find the below code. Failure.jsp is not calling in Chrome. Thanks in advance.

window.onbeforeunload = function() {
  window.location='Failure.jsp';
  confirm("onbeforeunload>>>>>>>>>"+window.location);
}

Failure.jsp:

<body>
<h3> <b> Your session Expired</b></h3>
<% session.invalidate();
System.out.println("invalidate>>>>>>>>>>>>>>>>>");
%>
Click here to <a href="http://localhost:8080/myApplication">Relogin</a>
</body>
gen_Eric
  • 223,194
  • 41
  • 299
  • 337
pitchaiah
  • 21
  • 1
  • 2
  • Please don't do this! `onbeforeunload` works fine in Chrome (as well as Firefox and IE), but it should just be used to tell the user they are going to leave the page, and maybe perform some cleanup. Chrome does not execute the `window.location` for security reasons. Some browsers may load that page (and you may see it because the `confirm` stops execution), but after `onbeforeunload` is done, the browser will continue with what it was doing (ie. loading the page the user wanted to go to). – gen_Eric Jul 22 '11 at 13:56

1 Answers1

3

The onbeforeunload event is (supposed to be) used for cleanup only, and debatably...prompting to save work. It's definitely not for you to force the browser to redirect to another page of your choosing, chrome actively blocks this behavior, for good reason.

An an aside: the event isn't specified, that is to say it can behave however it likes...and Chrome is choosing to protect the user here.

Nick Craver
  • 623,446
  • 136
  • 1,297
  • 1,155
  • He has a window.location in a confirm box as text, he isn't trying to redirect anything. – Clint Aug 25 '11 at 00:31
  • @Clint - He is, look at the code: `window.location='Failure.jsp';`, that's *trying* to redirect the user to `Failure.jsp` when they leave the page...that's a no-no. – Nick Craver Aug 25 '11 at 00:34