简体   繁体   English

包含JavaScript的JQuery.load()页面

[英]JQuery.load() page containingJavaScript

I have a html page which contains the some javascript to generate a chart from Google API. 我有一个html页面,其中包含一些可从Google API生成图表的JavaScript。 WHen I access the page directly, it works as expected, showing the chart. 如果我直接访问该页面,它将按预期工作,并显示图表。

<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load('visualization', '1.0', { 'packages': ['corechart'] });
google.setOnLoadCallback(drawChart);
function drawChart() {
   //// code to generate chart
   var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
   chart.draw(data, options);
}
</script>
<div id="chart_div">
   Chart Div
</div>

I want to load this chart into a on another page. 我想将此图表加载到另一页上的。 I tried using JQuery's $.get and $.load, but neither are working. 我尝试使用JQuery的$ .get和$ .load,但都无法正常工作。 IF I add some text to the page and call get/load i can see the text but not the chart - it seems the Javascript is not executing. 如果我在页面上添加了一些文本并调用get / load,我可以看到文本,但看不到图表-似乎Javascript没有执行。

I added alert('msg') and saw that the google.load got called, but no other JS was being called. 我添加了alert('msg')并看到google.load被调用,但是没有其他JS被调用。

From the other page where I want include the chart I have the following JS: 在要包含图表的其他页面上,我有以下JS:

$(document).ready(function () {
    $('#test').load('chart?param=1', function () {
        alert('Load was performed.');
    });
});

but the 'load was performed' never gets hit. 但是“负载已执行”从未受到打击。

"SCRIPT5009: 'google' is undefined" “ SCRIPT5009:'google'未定义”

I suspect that this <script type="text/javascript" src="https://www.google.com/jsapi"></script> isn't working when you .load() that block of code. 我怀疑当您.load()该代码块时,此<script type="text/javascript" src="https://www.google.com/jsapi"></script>无法正常工作。 Since jQuery is already running try this 由于jQuery已经在运行,请尝试以下操作

<!-- get rid of this <script type="text/javascript" src="https://www.google.com/jsapi" -->
<script type="text/javascript">
// does not need $(function()) unless you're running this on its own
// when loading this in using $.load() it's already ready.

$.getScript('https://www.google.com/jsapi', function(){
    google.load('visualization', '1.0', { 'packages': ['corechart'] });
    google.setOnLoadCallback(drawChart);
});
function drawChart() {
   //// code to generate chart
   var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
   chart.draw(data, options);
}
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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