How to return a value with JavaScript.getCurrent().execute()?

I am working with Vaadin 8.

My goal is to execute a JavaScript snippet and return a value so it is available in Java. Is this possible?

Thank you,

Oliver

You can use a JavaScript function callback, as described here: https://vaadin.com/docs/v8/framework/advanced/advanced-javascript.html#advanced.javascript.callback

Olli Tietäväinen:
You can use a JavaScript function callback, as described here: https://vaadin.com/docs/v8/framework/advanced/advanced-javascript.html#advanced.javascript.callback

Thank you for your answer.

But is there a way to do this on the client-side? I want to get the return value of the execution.

Or wait until the callback is finished?

If you use addFunction to add a JavaScript function, the value you pass as a parameter to the function will be available on the server-side callback.

Olli Tietäväinen:
If you use addFunction to add a JavaScript function, the value you pass as a parameter to the function will be available on the server-side callback.

But this is a callback. There’s no way to execute JS on the client-side and wait for it to complete and return a value to Java?

Not sure I understand. That sounds like what addFunction does. Do you have a concrete example?

I am sorry for being unclear. My professional Java career has only just begun and the server/client aspect of Vaadin is still new to me. So, thank you for your patience.

I have read https://vaadin.com/docs/v8/framework/advanced/advanced-javascript.html. But lets say I have a HTML page packaged with a war. I embed this page inside a BrowserFrame. This page contains a canvas which users will draw on. I want to call HTMLCanvasElement.toDataURL() on the client-side from Java, wait for the script to complete and return a value before continuing.

Maybe I need to create a new component using GWT’s Canvas class to have control over client-side functionality?

Thank you.

You can execute any JavaScript snippet with Page.getCurrent().getJavaScript().execute() that generates a value and use the registrated callback to send it to the server. I think you shouldn’t need any GWT code, assuming you are serving the iframe contents from the same origin as the main page.