1

I have MyPage.aspx html page (generated using ASP.Net). When user tries to navigate away from this page, I need to close the window – user should not be able to go back or navigate to another page.

When I used window.close() inside window.onbeforeunload event, it asks for a confirmation to the user. “The webpage you are viewing is trying to close the window. Do you want to close the window?” On clicking “No” the user can escape the close attempt. Is there any way to forcefully close the window without giving an option to the user?

Reference:

  1. How can I close a browser window without receiving the "Do you want to close this window" prompt?
  2. Html javascript to open new window and close current window
  3. "Unknown Exception" when cancelling page unload with "location.href"
  4. Display confirmation popup with JavaScript upon clicking on a link
Community
  • 1
  • 1
LCJ
  • 22,196
  • 67
  • 260
  • 418
  • You could improve the question by explaining why you want to prevent user from navigating back or forth. – Ja͢ck Oct 03 '12 at 08:38
  • @Jack I am trying to manually implement a LogOut feature for website – LCJ Oct 03 '12 at 08:39
  • 1
    A reliable, cross-browser solution for might be tough. You could disable client-side caching for the page so they can't navigate back to it from history. – moribvndvs Oct 03 '12 at 08:41
  • 1
    @Lijo So then just remove the cookies? Or if the cookies are HttpOnly, make a synchronous HTTP request to remove them. – Ja͢ck Oct 03 '12 at 08:45
  • @Jack Thanks for the input. I am planning to keep an indicator in database for Logout feature. In each post back I will update it as "Y". But when Logout is clicked, it will be updated as "N". When user click "Back", the indicator still will be "N" in the database. I will check the indicator status using jQuery.ajax. If it is "N" in this check, I will redirect to Logout page again. Do you think it is okay? – LCJ Oct 03 '12 at 11:05

3 Answers3

3

You can "trick" the browser like this:

window.onbeforeunload = function() {
    window.open('', '_self', '');
    window.close();
}

It seems to work in chrome/safari/ie/ff: http://jsbin.com/olijig/1

Firefox seems stubborn, but there might be another way to do the same in FF.

I should probably say that this technique is in no way standard and I don’t recommend it at all, and this code might break in many browsers besides firefox.

UPDATE

It actually works in Firefox too (latest version), but not older versions (I tried 3.6.1). You need to do some more testing to confirm the browser compatibility.

David Hellsing
  • 106,495
  • 44
  • 176
  • 212
1

No, you can't. The user must be always capable of controlling whatever happens in his browser.

Oscar
  • 13,594
  • 8
  • 47
  • 75
1

I'm not positive about this, but I believe if you have a window open another window, the parent window can close that child window. Would it be practical to have a landing page that opens your app in a separate window that could then close the window through javascript? Someone can probably elaborate more, as I haven't done this myself.

Adam Plocher
  • 13,994
  • 6
  • 46
  • 79