7

I am trying to call a javaScript function that's in .../js/index.js file to .../index.jsp file.

Any suggestion would be helpful.

Here is code within both file:

index.js

function testing() {

    if ("c" + "a" + "t" === "cat") {
        document.writeln("Same");
    } else {
        document.writeln("Not same");
    };
};

index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
</head>
<body>

    <script type="text/javascript" src="js/index.js">

       <!-- I want to call testing(); function here -->

    </script>
</body>
</html>
Danilo Valente
  • 11,270
  • 8
  • 53
  • 67
Simple-Solution
  • 4,209
  • 12
  • 47
  • 66
  • 2
    It doesn't make sense to directly call JavaScript from JSP. The JSP code runs on the server, and cannot run that sort of on-page JavaScript. By the time the processed page gets to the browser, there's no trace left of the JSP. You can, however, arrange for the function to be called when the page reaches the client browser, and that's pretty easy as the answers demonstrate. – Pointy Jul 07 '12 at 15:09
  • Mr. Dimitrov's answer below explains the situation. – Pointy Jul 07 '12 at 15:18
  • Is there anyhow I can load the .js file in .jsp file at runtime and then call specific .js function within .jsp file instead of making two calls to .js file i.e. – Simple-Solution Jul 07 '12 at 15:23
  • The answer Darin gives does not make two calls to the JavaScript file. It loads the file with one ` – Pointy Jul 07 '12 at 15:26

2 Answers2

19

First reference the external index.js file and then call the function in an inline script element:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
</head>
<body>
    <script type="text/javascript" src="js/index.js"></script>
    <script type="text/javascript">
       testing();
    </script>
</body>
</html>

By the way you have an error in your test.js function. You shouldn't put a ; after if/else conditions, neither at the end of your function declaration. The correct syntax is:

function testing() {
    if ("c" + "a" + "t" === "cat") {
        document.writeln("Same");
    } else {
        document.writeln("Not same");
    }
}

or:

var testing = function() {
    if ("c" + "a" + "t" === "cat") {
        document.writeln("Same");
    } else {
        document.writeln("Not same");
    }
};
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
-1

you can do like this

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<script type="text/javascript" src="js/index.js"></script>
<title>Insert title here</title>
</head>
<body>
<input type = "button" onclick = "javascript:testing()"/>
</body>
</html>

It is easiest