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");
}
}
}
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");
}
}
}
Share
Improve this question
edited Jul 25, 2012 at 4:29
Anuj Balan
asked Jul 24, 2012 at 9:47
Anuj BalanAnuj Balan
7,72923 gold badges61 silver badges94 bronze badges
2
- Use the console (built-in browser tool) to check for any errors. – Novak 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. – Novak Commented Jul 24, 2012 at 10:02
1 Answer
Reset to default 3check 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>