firefox - 谷歌图表 : google is not defined

标签 firefox google-api google-visualization

我正在尝试在 Firefox 上使用 google 图表,问题是 google 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 的引用,如 release notes 中所述...

The version of Google Charts that remains available via the jsapi loader is no longer being updated consistently. Please use the new gstatic loader (loader.js) from now on.



下一个 , 删除“验证”if声明来自 LoadGoogle ,这不应该是必需的,并且由于删除 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>

关于firefox - 谷歌图表 : google is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40201006/

相关文章:

javascript - 扩展和插件之间的通信

android - NearbyBackgroundBeacons 演示在订阅时返回未知状态代码 2801

android - 如何在安卓应用中进行谷歌图片搜索

php - 如何在 tcpdf 中包含 jpgraph

javascript - 如何设置/检测页面中浏览器 native 搜索生成的突出显示框

css - 加载 url firefox 失败

javascript - 如何在谷歌图表中显示两个不同的工具提示

google-chrome - 添加Google Charts或类似Chrome打包应用程序

javascript - 在 Firefox 中单击桌面通知时如何将焦点放在选项卡上?

asp.net - ASP.NET 中 Google API 中的 redirect_uri_mismatch