Browser's Information and Controls"
Maya001122 (talk | contribs) m (Created page with '{{ZKDevelopersGuidePageHeader}} To retrieve the information about the client, you can register an event listener for the <tt>onClientInfo</tt> event at a root component. To cont…') |
m (correct highlight (via JWB)) |
||
(One intermediate revision by one other user not shown) | |||
Line 1: | Line 1: | ||
{{ZKDevelopersGuidePageHeader}} | {{ZKDevelopersGuidePageHeader}} | ||
+ | {{Old Version | ||
+ | |url=http://books.zkoss.org/wiki/ZK_Developer%27s_Reference/UI_Patterns/Browser_Information_and_Control | ||
+ | |}} | ||
− | To retrieve the information about the client, you can register an event listener for the < | + | To retrieve the information about the client, you can register an event listener for the <code>onClientInfo</code> event at a root component. To control the behavior of the client, you can use the utilities in the <javadoc>org.zkoss.zk.ui.util.Clients</javadoc> class. |
=== The onClientInfo Event === | === The onClientInfo Event === | ||
− | Sometimes an application needs to know the client's information, such as time zone. Then, you can add an event listener for the < | + | Sometimes an application needs to know the client's information, such as time zone. Then, you can add an event listener for the <code>onClientInfo</code> event. Once the event is added, the client will send back an instance of the <javadoc>org.zkoss.zk.ui.event.ClientInfoEvent</javadoc> class, from which you can retrieve the information of the client. |
<source lang="xml" > | <source lang="xml" > | ||
Line 23: | Line 26: | ||
</source> | </source> | ||
− | '''Note''': The < | + | '''Note''': The <code>onClientInfo</code> event is meaningful only to the root component (aka., a component without any parent). |
The client information is not stored by ZK, so you have to store it manually if necessary. Since a session is associated with the same client, you can store the client info in the session's attribute. | The client information is not stored by ZK, so you have to store it manually if necessary. Since a session is associated with the same client, you can store the client info in the session's attribute. | ||
Line 29: | Line 32: | ||
session.setAttribute("px_preferred_time_zone", event.getTimeZone()); | session.setAttribute("px_preferred_time_zone", event.getTimeZone()); | ||
− | Notice that, if you store a time zone as a session variable called < | + | Notice that, if you store a time zone as a session variable called <code>px_preferred_time_zone</code>, then its value will be used as the default time zone thereafter. Refer to the '''Time Zone''' section in the '''Internationalization''' chapter. |
− | Notice that the < | + | Notice that the <code>onClientInfo</code> event is sent from the client after the page is rendered (and sent to the client). Thus, if some of your component's data depends on the client's info, say, time zone, you might have to ask the client to re-send the request as follows. |
<source lang="java" > | <source lang="java" > | ||
Line 58: | Line 61: | ||
</source> | </source> | ||
− | Once the < | + | Once the <code>confirmClose</code> method is called with a non-empty string, a confirmation dialog is shown up when the user tries to close the browser window, reload, or browse to another URL: |
[[Image:100000000000036D000000FE561CE3BC.png]] | [[Image:100000000000036D000000FE561CE3BC.png]] | ||
{{ ZKDevelopersGuidePageFooter}} | {{ ZKDevelopersGuidePageFooter}} |
Latest revision as of 10:35, 19 January 2022
This documentation is for an older version of ZK. For the latest one, please click here.
This documentation is for an older version of ZK. For the latest one, please click here.
To retrieve the information about the client, you can register an event listener for the onClientInfo
event at a root component. To control the behavior of the client, you can use the utilities in the Clients class.
The onClientInfo Event
Sometimes an application needs to know the client's information, such as time zone. Then, you can add an event listener for the onClientInfo
event. Once the event is added, the client will send back an instance of the ClientInfoEvent class, from which you can retrieve the information of the client.
<grid onClientInfo="onClientInfo(event)">
<rows>
<row>Time Zone <label id="tm"/></row>
<row>Screen <label id="scrn"/></row>
</rows>
<zscript>
void onClientInfo(ClientInfoEvent evt) {
tm.setValue(evt.getTimeZone().toString());
scrn.setValue(
evt.getScreenWidth()+"x"+evt.getScreenHeight()+"x"+evt.getColorDepth());
}
</zscript>
</grid>
Note: The onClientInfo
event is meaningful only to the root component (aka., a component without any parent).
The client information is not stored by ZK, so you have to store it manually if necessary. Since a session is associated with the same client, you can store the client info in the session's attribute.
session.setAttribute("px_preferred_time_zone", event.getTimeZone());
Notice that, if you store a time zone as a session variable called px_preferred_time_zone
, then its value will be used as the default time zone thereafter. Refer to the Time Zone section in the Internationalization chapter.
Notice that the onClientInfo
event is sent from the client after the page is rendered (and sent to the client). Thus, if some of your component's data depends on the client's info, say, time zone, you might have to ask the client to re-send the request as follows.
import org.zkoss.util.TimeZones;
...
if (!TimeZones.getCurrent().equals(event.getTimeZone())
Executions.sendRedirect(null);
The org.zkoss.ui.util.Clients Class
Utilities to control the client's visual presentation (more precisely, the browser window) are put in Clients collectively. For example, you can scroll the browser window (aka., the desktop) as follows.
Clients.scrollBy(100, 0);
Prevent User From Closing a Window
In some situation, you might want to prevent or, at least, alert a user when he tries to close the window or browse to another URL. For example, when a user is composing a mail that is not saved yet.
if (mail.isDirty()) {
Clients.confirmClose("Your message has not been sent.\nDiscard your message?");
} else {
Clients.confirmClose(null);
}
Once the confirmClose
method is called with a non-empty string, a confirmation dialog is shown up when the user tries to close the browser window, reload, or browse to another URL: