How to close the Browser Window using ZK framework?

734 Views Asked by At

I need to close the Browser Window Tab, please note not the window widget but the Browser window tab, using ZK framework version CE-9.0.0.

I have already tried the following code segments but no luck:

Clients.confirmClose(null);
Executions.deactivate(page.getDesktop());
page.setComplete(true);
page.removeComponents();
page.invalidate();

Is there any API or way to achieve this using ZK and/or JavaScript?

Is there any way to get the reference to the browser window tab, so that it can be closed programmatically, in ZK and/or JavaScript?

Please note: This window tab is opened manually & not by JavaScript or any other program.

Thanks,

RAS

2

There are 2 best solutions below

4
cor3000 On

The JS to close a window is window.close(), this is a well documented standard browser API. (also answered here)

From a ZK application (server-side) you can call this script via:

Clients.evalJavaScript("window.close()");

However this browser API has strict limitations: In modern browsers you can only close windows via close() that were also opened by javascript. So you can't close the current window in case the user navigated there manually - no way around that, except for using an old browser, which I won't recommend.

e.g. that's what you'll get in Chrome (#81)

log message in chrome

1
Hawk On

Close the browser tab from a server directly is not allowed. But you can show a modal window to ask a user to close a browser tab like:

   <zk xmlns:ca="client/attribute">
    ...
       <window mode="modal" title="Close the browser tab">
           <button label="Yes" ca:onClick="window.close();"/>
       </window>
   </zk>

Because close() is invoked by users, it will close the browser tab. This could be an alternative. Although it doesn't close a browser tab directly, the modal window covers the whole page. Users can't do anything but to click "yes", so I think most users will just click "yes" to close.

I found it works in Chrome 80, but will fail at Chrome 81. So it's not a good way