我试图在firefox上使用谷歌图表,问题是谷歌api不能在firefox上成功加载,只有ReferenceError: google is not defined
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script>
function LoadGoogle(){
if(typeof google != 'undefined' && google && google.load){
google.charts.load('current', {'packages':['corechart']});
// Set a callback to run when the Google Visualization API is loaded.
google.charts.setOnLoadCallback(drawChart);
// Callback that creates and populates a data table,
// instantiates the pie chart, passes in the data and
// draws it.
function drawChart() {
// Create the data table.
var data = new google.visualization.DataTable();
data.addColumn('string', 'Topping');
data.addColumn('number', 'Slices');
data.addRows([
['Mushrooms', 3],
['Onions', 1],
['Olives', 1],
['Zucchini', 1],
['Pepperoni', 2]
]);
// Set chart options
var options = {'title':'How Much Pizza I Ate Last Night',
'width':400,
'height':300};
// Instantiate and draw our chart, passing in some options.
var chart = new google.visualization.BarChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
}
else
{
console.log("try");
setTimeout(LoadGoogle, 30);
}
}
LoadGoogle();
</script>
首先,删除对jsapi
的引用,如发行说明中所述…
通过
jsapi
加载器仍然可用的Google Charts版本不再一致地更新。从现在开始请使用新的静态加载器(loader.js
)。
next,从LoadGoogle
中删除"validation"if
语句,这应该是不需要的,并且由于删除jsapi
而需要更改
下面的代码片段在Firefox ESR, v: 45.3.0
function LoadGoogle(){
google.charts.load('current', {
callback: drawChart,
packages:['corechart']
});
function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Topping');
data.addColumn('number', 'Slices');
data.addRows([
['Mushrooms', 3],
['Onions', 1],
['Olives', 1],
['Zucchini', 1],
['Pepperoni', 2]
]);
var options = {'title':'How Much Pizza I Ate Last Night',
'width':400,
'height':300};
var chart = new google.visualization.BarChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
}
LoadGoogle();
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>
您必须复制URL https://www.gstatic.com/charts/loader.js并将URL放在浏览器中,单击右键并保存为file.js将这两个文件保存在您的项目中,只需在html