The browser requests some data, for which I need to send back Javascript which will be executed on the browser.
How do I send Javascript (.js) back to the browser from a Play Framework controller?
Thanks
The browser requests some data, for which I need to send back Javascript which will be executed on the browser.
How do I send Javascript (.js) back to the browser from a Play Framework controller?
Thanks
If you just want to send back JavaScript, in your controller, you could do something like
public static void sayHello() {
renderText("alert('hello')");
}
Then in your JS function that you use to call the action (using jsAction tag), you just use the javascript eval function.
So, you code may look like
<script type="text/javascript">
var sayHello = #{jsAction @sayHello() /}
eval(sayHello());
</script>
Please note that eval is viewed as dangerous, as if someone managed to intercept the request, and injected their own javascript into your code, it would execute this code on the client's machine.