【问题标题】:include javascript in dynamically added jsp page在动态添加的jsp页面中包含javascript
【发布时间】:2014-06-16 00:13:11
【问题描述】:

我正在创建一个 j2ee 应用程序,我有 header.jsp、footer.jsp 和一个 result.jsp,这是我进行一些计算和显示结果的主页。 我正在使用 ajax 在 result.jsp 页面的 div 区域中包含页面 calculate.jsp 的内容。 calculate.jsp 页面正在使用 js 库中的一些 java 脚本函数,如果我将 java 脚本库包含在 header.jsp 中,它们不会被 calculate.jsp 拾取。它抛出错误:

Uncaught TypeError: undefined is not a function

如果我在 calculate.jsp 中包含 javascript 库,它可以正常工作。但我也想在其他页面中使用 js 库的功能。有没有办法在 header.jsp 页面中包含 js lib 并使其工作?

编辑:

我在 calculate.jsp 中绘制图表,这是代码:

<script>
 nv.addGraph(function() {
      var chart = nv.models.lineChart()
                    .width(750).height(370)
                    .margin({left: 100})  //Adjust chart margins to give the x-axis some breathing room.
                    .useInteractiveGuideline(true)  //We want nice looking tooltips and a guideline!
                    .transitionDuration(350)  //how fast do you want the lines to transition?
                    .showLegend(true)       //Show the legend, allowing users to turn on/off line series.
                    .showYAxis(true)        //Show the y-axis
                    .showXAxis(true)        //Show the x-axis
      ;      

      formatter = function(i){
          return dateArrBC[i];
      };

      chart.xAxis     //Chart x-axis settings
          .axisLabel('Date')
           .tickFormat(formatter); 

      chart.yAxis     //Chart y-axis settings
          .axisLabel('')
          .tickFormat(d3.format('.02f'));

      d3.select('#chart-area svg')    //Select the <svg> element you want to render the chart in.   
          .datum(data)         //Populate the <svg> element with chart data...
          .call(chart);          //Finally, render the chart!

      nv.utils.windowResize(function() { chart.update(); });
      return chart;
    });
</script>

它使用 addGraph 函数,如果包含在同一页面中(calculate.jsp)可以工作,但如果我在 header.jsp 中包含库,它会抛出找不到函数。 使用spring框架,所以ajax调用一个返回calculate.body的控制器函数

【问题讨论】:

  • 贴出相关代码
  • 你是如何加载页面的?也许你只是在 Script Execution 加载它的内容而不是它的脚本more info here
  • 如果您使用&lt;jsp:include.../&gt;,除非您的各种 JSP 文件位于不同的目录中,并且您的 javascript 被相对于无法从对方的位置。
  • 我添加了我的 js 代码,我正在使用 ajax 将另一个页面的内容包含在一个 div 中。

标签: java javascript jquery html jsp


【解决方案1】:

不,我不这么认为,因为calculate.jsp 是动态生成的,并且与 DOM 元素的 javascript 绑定发生在页面加载时。

因此,如果您尝试使用 calculate.jsp 中任何 js 文件中的任何 javascript 函数,则应将该 js 文件导入 calculate.jsp

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-30
    • 1970-01-01
    • 2011-06-15
    • 2016-02-19
    • 2017-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多