调用驻留在不同文件夹中的 javascript 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11628242/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calling javascript file residing in a different folder
提问by Anuj Balan
Am new to javascript. Am trying to call an external javascript file from jsp.
我是 javascript 新手。我正在尝试从 jsp 调用外部 javascript 文件。
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.
我在执行上述操作时没有问题,但是当我尝试将此javascript 放在不同的文件夹中然后调用时,我无法做到。
Code which calls js from jsp
从jsp调用js的代码
<input type="submit" name="submit" value="Submit" onclick="validate(this.form)"></input>
Added these lines in the headpart of the jsp
在jsp的head部分添加了这些行
<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.
结构:ServletsApp 是 WebContent 所在的主要项目文件夹。WebContent 有文件夹视图和脚本。我的 jsp 驻留在脚本文件夹中的视图和 .js 中。
Update:LoginExample.jsp
更新: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
验证.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");
}
}
}
回答by vikrantx
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>