2

This question is brought up because of this. The problem is, probably, the page is designed to generate a confirmation alert only if the user click on browser X button. I know that window.close() will close the current browser window which probably is not the same as clicking the X of browser window. Is there any way, possibly with JavaScript to recreate the click action on browser X button as if a real user would do?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Saifur
  • 16,081
  • 6
  • 49
  • 73
  • No there is not. `window.close()` will only work in child windows/tabs. You cannot close the parent browser window. – Rory McCrossan Mar 14 '15 at 22:58
  • no thats dumb, what if someone is doing something important on another tab. What if you have a bunch of tabs open, and you visit a website that runs that code and closes all your window. You would be pissed. The only way would be a chrome extension or other browser extension. – Edwin Reynoso Mar 14 '15 at 22:58
  • 1
    [Run JavaScript code on window close](http://stackoverflow.com/a/13443562/4343318) – Santiago Hernández Mar 14 '15 at 23:00
  • @RoryMcCrossan @Edwin I know that. But, My question is, say only for current *tab*, what is the difference between clicking **X** and `window.close()`? – Saifur Mar 14 '15 at 23:01
  • @callback Haven't tried. Going to. But what's the difference? – Saifur Mar 14 '15 at 23:02
  • Assuming the tab is allowed to be closed programmatically by the browser, the difference is that `window.close()` would close the tab, whereas clicking X would close the entire browser program. – Rory McCrossan Mar 14 '15 at 23:02
  • @RoryMcCrossan then, there is no way to perform click **X** with `JavaScript`? I am aware that that should not be done. But, it's needed for testing purpose only – Saifur Mar 14 '15 at 23:04
  • 1
    Correct, you cannot close the browser window with JS. – Rory McCrossan Mar 14 '15 at 23:05
  • @RoryMcCrossan Appreciate your time – Saifur Mar 14 '15 at 23:05
  • This may be the wrong question, if the intent is to offer the same confirmation dialog on either the browser's `onbeforeunload` event and/or whichever event (may be?) triggered by `window.close()`. I suspect since it's Javascript invoking the latter, the former is not invoked in a way where it's preventable (since it's not user-originating), so you don't get the dialog. Whatever is calling the `window.close()` should call the confirmation code directly before doing the close. – Jared Farrish Mar 14 '15 at 23:06
  • This [answer](http://stackoverflow.com/a/21577501/451969) suggests a solution that works on `window.close()` by using the `unload` event in addtion to `onbeforeunload`. The answeree claims it works in all browsers, but I presume Selenium was not tested. (Although looking at it, it may not work to prevent the close, if that's necessary.) – Jared Farrish Mar 14 '15 at 23:26

1 Answers1

1
window.close()

This method is only allowed to be called for windows that were opened by a script using the window.open() method. If the window was not opened by a script, the following error appears in the JavaScript Console: Scripts may not close windows that were not opened by script. Mozilla Developer Network: window.close()

So, there is not a way to close browser via javascript, but if you wan't to tigger some code that is supposed to be executed when window closes try this:

//plain js
window.onunload();

//plain js
window.onbeforeunload();

//jQuery
$(window).unload();

Or if you want to execute code when user tries to close window, try this:

window.onbeforeunload = function(){
   // Do something
}
// OR
window.addEventListener("beforeunload", function(e){
   // Do something
}, false);

window.onunload = function(){
   // Do something
}
// OR
window.addEventListener("unload", function(e){
   // Do something
}, false);

// jQuery
$(window).unload(function(){
   // Do whatever you need
});

Since 25 May 2011, the HTML5 specification states that calls to window.alert(), window.confirm(), and window.prompt() methods may be ignored during this event. See the HTML5 specification for more details. MDN | window.onbeforeunload

These events are fired in this order:

  1. window.beforeunload (cancellable event)
  2. window.pagehide
  3. window.unload
Santiago Hernández
  • 5,438
  • 2
  • 26
  • 34
  • Thanks for your answer. I know it was called by `window.open()` or at least something similar. Because `window.close()` closed the browser. But, If I perform click on **X** there is another pop up shows up to confirm the action which I do not get using `selenium`. I want to recreate the same with some kind of programming – Saifur Mar 14 '15 at 23:10