8

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>
4
  • 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. Commented Jul 7, 2012 at 15:09
  • Mr. Dimitrov's answer below explains the situation. Commented Jul 7, 2012 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. <script></script> Commented Jul 7, 2012 at 15:23
  • The answer Darin gives does not make two calls to the JavaScript file. It loads the file with one <script> tag, and then has code to call the function defined in that file with another <script> tag. You can't call the JavaScript code directly at the time the JSP is being processed on the server. There are ways to involve JavaScript in server-side Java processing, but it's complicated and would require the creation of some significant amount of infrastructure. Commented Jul 7, 2012 at 15:26

2 Answers 2

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");
    }
};
Sign up to request clarification or add additional context in comments.

Comments

-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

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.