最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Google is not defined using Google Visualization API; possibly jQuery's fault - Stack Overflow

programmeradmin2浏览0评论

I'm getting the same error as seen in this question to which there is no answer. To elaborate, I'm trying to load this demo in my code. I've altered it slightly in that I am not including their code in any header tag - this particular code fragment will be loaded in by jQuery. Anyway, so my code looks like this:

<script type='text/javascript' 
        src='....'>
</script>

<script type='text/javascript'>

function drawChart() {
    var data = new google.visualization.DataTable();
    data.addColumn('string', '', 'Country');
    data.addColumn('number', 'Population (mil)', 'a');
    data.addColumn('number', 'Area (km2)', 'b');
    data.addRows(5);
    data.setValue(0, 0, 'CN');
    data.setValue(0, 1, 1324);
    data.setValue(0, 2, 9640821);
    data.setValue(1, 0, 'IN');
    data.setValue(1, 1, 1133);
    /* ... */
    var chart = new google.visualization.IntensityMap(
                  document.getElementById('chart_div'));
    chart.draw(data, {});
}

$(document).ready(function() {
    google.load('visualization', '1', {packages:['intensitymap']});
    google.setOnLoadCallback(drawChart);
});
</script>

This section of code lies in a div whose visibility is toggled as needed. The whole lot (the entire page here) is returned as the result of an ajax call.

The theory here being using jQuery's $(document).ready() handler means that google should be loaded when the document is ready.

However, I'm getting this:

Regardless of whether that section is inside ready() or not. Now here's the real kicker: in the dom explorer, I can find said object:

Can anyone please explain to me firstly why this is happening and then what I do to fix it?

Being a naive kind of javascript developer, I tried including the google scripts in my head tags. That then produced something like this question ($ not defined) except that we're not loading jQuery from google, we're hosting it locally.

We successfully load a number of other jQuery extensions inline this way as well as extra parts of jQuery code, so to my mind this should work. I do not know whether jQuery is getting in the way of Google/vice versa but they shouldn't be.

Thoughts?

I'm getting the same error as seen in this question to which there is no answer. To elaborate, I'm trying to load this demo in my code. I've altered it slightly in that I am not including their code in any header tag - this particular code fragment will be loaded in by jQuery. Anyway, so my code looks like this:

<script type='text/javascript' 
        src='https://www.google.com/jsapi?key=ABQIAAAAKl2DNx3pM....'>
</script>

<script type='text/javascript'>

function drawChart() {
    var data = new google.visualization.DataTable();
    data.addColumn('string', '', 'Country');
    data.addColumn('number', 'Population (mil)', 'a');
    data.addColumn('number', 'Area (km2)', 'b');
    data.addRows(5);
    data.setValue(0, 0, 'CN');
    data.setValue(0, 1, 1324);
    data.setValue(0, 2, 9640821);
    data.setValue(1, 0, 'IN');
    data.setValue(1, 1, 1133);
    /* ... */
    var chart = new google.visualization.IntensityMap(
                  document.getElementById('chart_div'));
    chart.draw(data, {});
}

$(document).ready(function() {
    google.load('visualization', '1', {packages:['intensitymap']});
    google.setOnLoadCallback(drawChart);
});
</script>

This section of code lies in a div whose visibility is toggled as needed. The whole lot (the entire page here) is returned as the result of an ajax call.

The theory here being using jQuery's $(document).ready() handler means that google should be loaded when the document is ready.

However, I'm getting this:

Regardless of whether that section is inside ready() or not. Now here's the real kicker: in the dom explorer, I can find said object:

Can anyone please explain to me firstly why this is happening and then what I do to fix it?

Being a naive kind of javascript developer, I tried including the google scripts in my head tags. That then produced something like this question ($ not defined) except that we're not loading jQuery from google, we're hosting it locally.

We successfully load a number of other jQuery extensions inline this way as well as extra parts of jQuery code, so to my mind this should work. I do not know whether jQuery is getting in the way of Google/vice versa but they shouldn't be.

Thoughts?

Share Improve this question edited May 23, 2017 at 12:00 CommunityBot 11 silver badge asked Apr 5, 2011 at 18:52 user257111user257111
Add a comment  | 

2 Answers 2

Reset to default 11

Removing $(document).ready should fix your problem. I tried your code and after commenting out $(document).ready, it worked:

//$(document).load(function() {
    google.load('visualization', '1', {packages:['intensitymap']});
    google.setOnLoadCallback(drawChart);
//});

Now as for why this is the case, I do not know... In any case, you shouldn't need to wait for document.ready to call google.load; google.load will ensure that by the time your callback drawChart is called, it will be safe to execute.

You can load the the charts after the page has loaded but with a special callback.

google.load("visualization", "1", {"callback" : drawChart});

https://developers.google.com/loader/#Dynamic

发布评论

评论列表(0)

  1. 暂无评论