在jsp页面中包含外部java脚本文件
我有一个名为 paging.js 的外部 JavaScript 文件。以下是该文件的内容:
function Pager(tableName,itemPerPage){
this.tableName = tableName;
this.itemPerPage = itemPerPage;
this.currentPage = 1;
this.pages = 0;
this.init()= function(){
alert("init called ");
var rows = document.getElementById(tableName).rows;
var records = (rows.length - 1);
this.pages = Math.ceil(records / itemPerPage);
}
this.showPageNav = function(pagerName,positionId){
alert("show page navi call");
var element = document.getElementById(positionId);
var pagerHtml = '<input src = "next.jpg" type="image">';
pagerHtml += '<input src = "next.jpg" type="image">' ;
element.innerHTML = pagerHtml;
}
}
现在我尝试从我的 jsp 页面调用 init,例如 .
<script type="text/javascript">
var pager = new Pager('results',7);
pager.init();
</script>
这段代码是我在完成我的 jsp 页面中的正文部分之前放置的。
为了包含此页面,我放置了类似“
<script type="text/javascript"
src="${pageContext.request.contextPath}/js/paging.js"></script>
但我无法调用 init 方法”的行。有没有人帮我发现问题?
I have an external JavaScript file named paging.js. Following are the contents of the file:
function Pager(tableName,itemPerPage){
this.tableName = tableName;
this.itemPerPage = itemPerPage;
this.currentPage = 1;
this.pages = 0;
this.init()= function(){
alert("init called ");
var rows = document.getElementById(tableName).rows;
var records = (rows.length - 1);
this.pages = Math.ceil(records / itemPerPage);
}
this.showPageNav = function(pagerName,positionId){
alert("show page navi call");
var element = document.getElementById(positionId);
var pagerHtml = '<input src = "next.jpg" type="image">';
pagerHtml += '<input src = "next.jpg" type="image">' ;
element.innerHTML = pagerHtml;
}
}
Now I tried to call init from my jsp page like .
<script type="text/javascript">
var pager = new Pager('results',7);
pager.init();
</script>
This code i put before complete my body part in my jsp page.
For including this page I put line like
<script type="text/javascript"
src="${pageContext.request.contextPath}/js/paging.js"></script>
But i can't able to call init method. Is there anyone to help me for finding problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这行代码就是问题所在:
修改为:
This line of code is the problem:
Change it to:
尝试
Try
借助 .jsp 2.+ 技术,我将所有链接和脚本放在一个单独的文件中,并使用
指令引用该文件:我的 links_and_scripts 页面具有此
meta 和我的脚本的路径:
With .jsp 2.+ technology, I place all my links and scripts in a separate file that I reference using the
<jsp:include>
directive:My links_and_scripts page has this
meta
and the path to my script: