Jan Chabik
Jan Chabik

Reputation: 99

Play cannot reference external javascript

A very basic question

Cannot load external javascript resource on server I am working on a Play framework project. I've made some basic html view with some Javascript. It works correctlly when I have my js code in the actual view. However, when I tried moving js code to a separate file and load it using

<script> src="main.js" </script> 

It works correctly when opened using plain chrome browser. However when I run it on server and it fails and chrome dev console prints the following message

GET http://localhost:9000/main.js 404 (Not Found)

I've tried setting up a GET request on targer URL but cannot pass main.js as an arguement to Ok method

def getmainJs()= Action {
    Ok()
  }

Is there a painless way to access the js code or do I have to go through the process of setting up the JavacriptRouter menntioned here. The app is only going to be 2 views to I kind of don't care about scalability

Upvotes: 0

Views: 109

Answers (1)

ygor
ygor

Reputation: 1756

I created an example on how to serve a Javascript file:

Routes:

GET         /foo                 sk.ygor.stackoverflow.q53319493.controller.ApplicationController.foo
GET         /assets/*file        controllers.Assets.versioned(file)

View:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
        <script src="@routes.Assets.versioned("main.js")"></script>
    </body>
</html>

Upvotes: 1

Related Questions