0

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

RAS
  • 8,100
  • 16
  • 64
  • 86

2 Answers2

0

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

cor3000
  • 936
  • 1
  • 6
  • 16
  • First of all, your assumption is wrong. I have gone through the documentation of each of the code that I have tried. Reason for trying them out is the multiple answers that are posted on various forums found on the Internet. Not only that, I have also gone through the links you posted already. Reason for asking the question is that I'm looking for solution specifically in ZK framework. This kind of question I could not find anywhere till now. Anyways, thanks for your reply. – RAS Apr 09 '20 at 06:55
  • ZK is running inside the browser, so that's the first place to look at, which APIs do browsers provide. If such APIs exist then there might be a matching convenience method in a framework such as ZK. – cor3000 Apr 09 '20 at 06:57
  • I removed my "snarky" comment. – cor3000 Apr 09 '20 at 07:17
  • I understand your point. So in that case I don't have any option to close the browser tab. :( – RAS Apr 09 '20 at 08:24
0

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

Hawk
  • 897
  • 7
  • 14
  • 2
    I found it works in Chrome 80, but will fail at Chrome 81. So it's not a good way. – Hawk Apr 10 '20 at 10:41