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

发布于 2024-10-13 21:18:35 字数 1281 浏览 2 评论 0原文

我有一个名为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

余生共白头 2024-10-20 21:18:35

这行代码就是问题所在:

this.init()= function(){

修改为:

this.init=function() {

This line of code is the problem:

this.init()= function(){

Change it to:

this.init=function() {
埋葬我深情 2024-10-20 21:18:35

尝试

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

Try

<script type="text/javascript" 
                  src="js/paging.js"></script>
爱的那么颓废 2024-10-20 21:18:35

借助 .jsp 2.+ 技术,我将所有链接和脚本放在一个单独的文件中,并使用 指令引用该文件:

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

我的 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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文