最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - include external java script file in jsp page- Stack Overflow

programmeradmin0浏览0评论

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 plete 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?

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 plete 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?

Share Improve this question edited Jan 16, 2012 at 12:29 javanna 60.2k14 gold badges147 silver badges125 bronze badges asked Jan 25, 2011 at 6:49 chetanchetan 3,25520 gold badges74 silver badges114 bronze badges 3
  • How you are not able to call init? Have you tried in firebug? Can you see the javascript file loaded in browser? – TheVillageIdiot Commented Jan 25, 2011 at 6:56
  • yes if i put simple alert int the page its work fine – chetan Commented Jan 25, 2011 at 7:28
  • Yes you are right ,its work on IE.But why simple alert work and this not work. – chetan Commented Jan 25, 2011 at 7:37
Add a ment  | 

3 Answers 3

Reset to default 1

This line of code is the problem:

this.init()= function(){

Change it to:

this.init=function() {

Try

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

With .jsp 2.+ technology, I place all my links and scripts in a separate file that I reference using the <jsp:include> directive:

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

My links_and_scripts page has this meta and the path to my script:

<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
发布评论

评论列表(0)

  1. 暂无评论