3

Am new to javascript. Am trying to call an external javascript file from jsp.

I have no problem in doing the above but when I tried to place this javascript in a different folder and then call, am not able to do it.

Code which calls js from jsp

<input type="submit" name="submit" value="Submit" onclick="validate(this.form)"></input>

Added these lines in the head part of the jsp

<script src="scripts/validate.js">
</script>

Structure: ServletsApp is the main project folder in which WebContent is present. WebContent has folders views and scripts. My jsp resides in views and .js in scripts folder.

Update: LoginExample.jsp

<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Share Market Application</title>
<script src="../WebContent/scripts/validate.js">
</script>
</head>

<body>
<form name="loginForm">
    <h2>Enter user credentials</h2>
    <br> 
    <font size="3" style="TimesNewRoman">Username:</font> 
    <input type="text" name="userId"></input> 
    <br>
    <font size="3" style="TimesNewRoman">Password:</font> 
    <input type="password" name="password"></input> 
    <br> 
    <input type="submit" name="submit" value="Submit" onclick="validate(this.form)"></input>
    <input type="button" name="cancel" value="Cancel"></input>
</form>

Validate.js

function validate(form)
{
alert("Entered");
alert(document.getElementById("userId"));
var userName=form.userId.value;
alert(userName);
var pwd=form.password.value;
alert(document.getElementById("userId").valueOf());
if(userName=="" || userName!="admin")
    {
    alert("User name is incorrect");
    }
else
    {
    if(pwd=="" || pwd!="admin")
    {
    alert("Password is incorrect");
    }   
    }

}
2
  • Use the console (built-in browser tool) to check for any errors. Commented Jul 24, 2012 at 9:54
  • Uhm, since your last response was so informative, I'll suggest installing Firebug. If you have any difficulties with it, download Chrome, it has a built-in console. Commented Jul 24, 2012 at 10:02

1 Answer 1

3

check proper path of file

<script src="~/Scripts/validate.js" type="text/javascript"></script>

try this

<script src="~/WebContent/Scripts/validate.js" type="text/javascript"></script>

or

<script src="../Scripts/validate.js" type="text/javascript"></script>
Sign up to request clarification or add additional context in comments.

5 Comments

An alert which I inserted, got executed the first time but from next time onwards, it aint calling again.
@AnujBalan debug using firebug
(/MyApp/views/~/Scripts/validate.js) is not available. Its looking for scripts folder inside views folder I think. But scripts and views are subfolders of a parent folder
try this <script src="../Scripts/validate.js" type="text/javascript"></script>
Tried it but now its searching for 'scripts' folder outside the parent folder

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.