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

在动态添加的jsp页面中包含javascript,java,javascript,jquery,html,jsp,Java,Javascript,Jquery,Html,Jsp,我正在创建一个j2ee应用程序,我有header.jsp、footer.jsp和一个result.jsp,它是我进行一些计算和显示结果的主页。 我使用ajax将页面calculate.jsp的内容包含在result.jsp页面的div区域中。 jsp页面正在使用js库中的一些java脚本函数,如果我将java脚本库包含在header.jsp中,则calculate.jsp不会提取这些函数。它抛出错误: Uncaught TypeError: undefined is not a function

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

Uncaught TypeError: undefined is not a function
如果我在calculate.jsp中包含javascript库,它就可以正常工作。但我也想在其他页面中使用js库的功能。有没有办法在header.jsp页面中包含js库并使其正常工作

编辑:

我正在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函数,若包含在同一个pagecalculate.jsp中,该函数可以工作,但若我将库包含在header.jsp中,它会抛出找不到的函数。 使用spring框架,ajax调用一个控制器函数,返回calculate.body

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


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

发布相关代码。您是如何加载页面的?也许你只是在脚本执行时加载了它的内容,而不是它的脚本如果你正在使用的话,你不应该有这个问题,除非你的各种JSP文件在不同的目录中,并且你的javascript被一个相对于另一个路径的路径引用,而这个路径从另一个位置不起作用。我添加了我的js代码,我正在使用ajax将另一个页面的内容包含在一个div中。