0

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

1
  • How do you intend to use the javascript? As JSON-encoded data? As code to be executed? Embedded in a HTML document? Commented Sep 15, 2012 at 4:44

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This is good advice, if you want to know a little more about what happens behind the scenes google ajax, xhr etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.