0

If I have something like this in Application.scala.html

<div id="action">Some content</div>

<script>
    $('action').click(function(){
         window.open(@routes.Application.index());
    )};
</script>

This works fine, no problem.

But if I put the script in its own Javascript file, I can no longer use the @routes.Application.index() reference. Is is possible in Play to reference Scala templates in a separate Javascript file place under public/javascripts?

2

1 Answer 1

1

Have you considered something like this?

<div id="action" data-target="@routes.Application.index()">Some content</div>

<script>
    $('action').click(function(){
         var target = $(this).data('target');
         window.open(target);
    )};
</script>

You can then extract the JavaScript to its own file.

Although in this particular case an <a> tag would be more appropriate (for a number of reasons, including accessibility).

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

Comments

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.