0

Access javascript window from Groovy

asked 2017-11-10 22:31:41 +0800

fmarzulli gravatar image fmarzulli
1

updated 2017-11-13 09:49:41 +0800

cor3000 gravatar image cor3000
3494 1 7
ZK Team

Hi, I'm trying to get the current url using groovy. The code is:

String script = """
        function getCurrentUrl(){
        return window.location.href; 
        }
        """

def engine = new ScriptEngineManager().getEngineByName('javascript')
engine.eval(script)
Invocable inv = (Invocable) engine
def response = inv.invokeFunction("getCurrentUrl")

But I get

org.zkoss.zk.ui.UiException: javax.script.ScriptException: ReferenceError: "window" is not defined in <eval> at line number 3
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.zkoss.lang.Classes.newInstance(Classes.java:74)
    at org.zkoss.lang.Exceptions.wrap(Exceptions.java:163)
    at org.zkoss.zk.ui.UiException$Aide.wrap(UiException.java:51)
    at org.zkoss.bind.impl.ParamCall.call(ParamCall.java:157)
    at org.zkoss.bind.impl.BinderImpl.doExecute(BinderImpl.java:2035)
    at org.zkoss.bind.impl.BinderImpl.doCommand(BinderImpl.java:1748)
    at org.zkoss.bind.impl.BinderImpl.access$1300(BinderImpl.java:131)

Any idea?

delete flag offensive retag edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2017-11-13 10:09:25 +0800

cor3000 gravatar image cor3000
3494 1 7
ZK Team

What you are doing looks like you are executing a JS function at server side which will run in a JS engine inside your JVM. Even though it's JS it is not running in the browser - and that's why the window variable is not defined.

Assuming you want the URL in the browser then you have to send a script back to the browser to evaluate, and then fire an event back to the server in order to get the result.

To me this only sounds necessary when the client side URL changes during the lifetime of your ZK Desktop or if it is changed transparently by a proxy in the middle and the real browser URL is needed.

If it doesn't change and you are only interested in the requestPath and queryString the straight forward way is to call (in Java - I don't know the corresponding groovy out of my head)

Executions.getCurrent().getDesktop().getRequestPath();
Executions.getCurrent().getDesktop().getQueryString();

I can elaborate more on the first option if you think the information stored in the desktop object is not sufficient for your needs.

Robert

link publish delete flag offensive 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

Follow
2 followers

RSS

Stats

Asked: 2017-11-10 22:31:41 +0800

Seen: 9 times

Last updated: Nov 13