Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import javascript into html on play 2.0 framework

I'm having problems including local javascript files into my html that is on the play framework. The paths are correct and I even tried including the javascript file in the same directory. However, imports from the web (the main libraries i'm using) work just fine.

@(execId: String)

<html>

<head>
<title>Timeline</title>

<script type="text/javascript" src="http://mbostock.github.com/d3/d3.js"></script>



<script type="text/javascript"

src="http://code.jquery.com/jquery-latest.js"></script>

<script type = "text/javascript" src = "../../public/javascripts/profilesJS/stack.js">  </script>


</head>


<body>
<input id="profiles" type="button" value="Profiles" />
<script type="text/javascript">
        alert(tester());
    </script>

</body>


</html>

the javascript file simply looks likes this

function tester(){

return "test";

}

And the error i get is:

tester is not defined

at the line with the alert

like image 814
user1470463 Avatar asked Jun 20 '12 21:06

user1470463


People also ask

How do I import a JavaScript file into HTML?

To include an external JavaScript file, we can use the script tag with the attribute src . You've already used the src attribute when using images. The value for the src attribute should be the path to your JavaScript file. This script tag should be included between the <head> tags in your HTML document.

Can we import JS file in CSS?

No, you can only import CSS stylesheets or . less and . css stylesheets when using Less. You can do it the other way around however and import CSS stylsheets with JavaScript.

How do I connect to a JavaScript file?

We can include a JavaScript file in another JavaScript file using the native ES6 module system. This allows us to share code between different JavaScript files and achieve modularity in the code. There are other ways to include a JS file like Node JS require, jQuery's getScript function, and Fetch Loading.


1 Answers

According to the assets documentation (and routing in general) you need to use the reverse routing in your template:

<script type="text/javascript" src='@routes.Assets.at("javascripts/profilesJS/stack.js")'></script>

it builds the correct src path to your /public/javascripts/profilesJS/stack.js file (by default routing config it will be /assets/javascripts/profilesJS/stack.js)

like image 111
biesior Avatar answered Sep 22 '22 12:09

biesior