Clients eval javascript

asked 2021-03-13 00:28:19 +0800

aUser gravatar image aUser
121 4


I have a java command method and after updating a model i'd like to apply some styling to a widget after this is attached to the DOM tree. It looks something like this:

public void updateModel() {
    // updating model..
    Clients.evalJavaScript("zk.Widget.$('$" + aComp.getId() + "').focus(); ");

Inside the zul page i have a standard template:

<div children="@load(each.pages) @template('images')" >
    <template name="images" >
        <a  id="@load(each.id)">

So after the model is updated i'd like to put focus on a particular widget but i need for it to be attached to the DOM tree first. I don't think Clients.evalJavaScript("code") waits for that since in the js console in the browser i can see that widget is null. Is there a way to wait for the DOM tree to be "completed" and only then apply the javascript code? I need this to be done every time i update the model, so using just a script like this

<script type="javascript/text" defer="true">
    $(function() {

won't be enough since this will be evaluated only once.

ZK Version is


delete flag offensive retag edit

1 Answer

Sort by ยป oldest newest most voted

answered 2021-03-17 12:10:24 +0800

cor3000 gravatar image cor3000
5833 2 7
ZK Team

updated 2021-03-19 10:12:41 +0800

did you try the dedicated Clients.focus(...) API (new in ZK 9.5) methods to focus and scroll into view ?

UPDATE 2021-03-18

here an example updating the focus after children have been created (both initially and after a command replacing the children list)


The delayed execution is achieved by posting an event with a lower priority (e.g. -1) to happen after child components are created by the children binding

link publish delete flag offensive edit


Not yet.. I'll try it asap, thank you. Can i assume that i can use this method from a java command and be sure that the focus/scrollIntoView will be called once the DOM is ready? My biggest concern is that having a lot of elements (say images) to render the focus might be lost at some point..

aUser ( 2021-03-17 17:56:40 +0800 )edit

I tried the new version but i have a problem with the method BindUtils.postNotifyChange(..). It seems that Clients.evalJavaScript or any other method of the Clients class is executed before the NotifyChange is actually done (i'm assuming both java function and annotation).

aUser ( 2021-03-18 00:42:37 +0800 )edit

In this case i would need the Clients class to execute its methods after the notify change is done, but i'm not sure if that is even possible..

aUser ( 2021-03-18 00:43:38 +0800 )edit

yes it is possible - see update

cor3000 ( 2021-03-18 15:29:04 +0800 )edit

Thanks cor3000! It works fine. If i have to post two events with priority 2 and 1.. the one with priority 2 will be executed first but can i wait for it to finish and only then execute event with priority 1? I know this is possible with javascript using async and await, but is it possible from java?

aUser ( 2021-03-19 00:01:46 +0800 )edit
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

1 follower



Asked: 2021-03-13 00:28:19 +0800

Seen: 9 times

Last updated: Mar 19

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More