简体   繁体   English

在jsp页面中包含外部Java脚本文件

[英]include external java script file in jsp page

I have an external JavaScript file named paging.js. 我有一个名为pageing.js的外部JavaScript文件。 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 . 现在,我尝试从jsp页面调用init,例如。

<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. 我在完成我的jsp页面中的正文部分之前放置的这段代码。

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. 但是我无法调用init方法。 Is there anyone to help me for finding problem? 有没有人可以帮助我发现问题?

This line of code is the problem: 这行代码就是问题所在:

this.init()= function(){

Change it to: 更改为:

this.init=function() {

With .jsp 2.+ technology, I place all my links and scripts in a separate file that I reference using the <jsp:include> directive: 使用.jsp 2. +技术,我将所有链接和脚本放在使用<jsp:include>指令引用的单独文件中:

<jsp:include page="//path to your links_and_scripts page">

My links_and_scripts page has this meta and the path to my script: 我的links_and_scripts页面具有此meta和脚本的路径:

<meta http-equiv="Content-Script-Type" content="application/javascript; charset=utf-8" />
<script src="// path to your scripts js"></script>
//...your other scripts and links here

Try 尝试

<script type="text/javascript" 
                  src="js/paging.js"></script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM