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?

cYn
  • 3,291
  • 6
  • 25
  • 43
  • [Play Framework - Using Javascript Variable in Scala Template](http://stackoverflow.com/a/17817560/651140) – Andrzej Jozwik Oct 14 '14 at 18:20
  • Play used twirl. Check twirl documentation: [twirl](https://github.com/playframework/twirl/blob/master/README.md). There is: `Template files must be named {name}.scala.{ext} where ext can be html, js, xml, or txt.` – Andrzej Jozwik Oct 14 '14 at 18:23

1 Answers1

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).

danielnixon
  • 4,178
  • 1
  • 27
  • 39