在动态添加的jsp页面中包含javascript

时间:2014-04-29 20:59:11

标签: java javascript jquery html jsp

我正在创建一个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

的控制器函数

1 个答案:

答案 0 :(得分:0)

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

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

相关问题