简体   繁体   中英

include javascript in dynamically added jsp page

I am creating a j2ee application, I have header.jsp, footer.jsp and one result.jsp which is the main page where I am doing some calculations and showing results. I am using ajax to include content of a page calculate.jsp in a div area in result.jsp page. calculate.jsp page is using some java script functions from a js library, the java script library if i include in header.jsp they are not picked up by calculate.jsp. It throws error :

Uncaught TypeError: undefined is not a function

If I include javascript library in calculate.jsp it works fine. But I want to use function of js library in other pages also. Is there a way to include js lib in header.jsp page and make it work?

Edit:

I am ploting graphs in calculate.jsp this is the code :

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

Its using addGraph function which if included in same page(calculate.jsp) works but if I include library in header.jsp it throws function not found. Using spring framework, so ajax calls a controller function which returns calculate.body

No i don't think so as calculate.jsp is dynamically generated and javascript binding with DOM elements happens at the time of page loading.

So if your are trying to use any javascript function from any js file in calculate.jsp you should import that js file in calculate.jsp

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM