Google 不是使用 Google Visualization API 定义的;可能是 jQuery 的错

发布于 2024-10-30 10:52:54 字数 2054 浏览 1 评论 0原文

我遇到了与 中看到的相同错误这个问题没有答案。为了详细说明,我正在尝试加载此演示我的代码。我稍微修改了一下,因为我没有将它们的代码包含在任何标头标记中 - 这个特定的代码片段将由 jQuery 加载。无论如何,我的代码如下所示:

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

这部分代码位于一个 div 中,其可见性根据需要进行切换。整个批次(这里是整个页面)作为 ajax 调用的结果返回。

这里使用 jQuery 的 $(document).ready() 处理程序的理论意味着当文档准备好时应该加载 google。

但是,我得到了这个:

Google 未定义错误消息屏幕截图。

无论该部分是否位于 内是否准备好()。现在真正的亮点是:在 dom 资源管理器中,我可以找到所述对象:

So google 确实存在。这张图片证明了这一点。

谁能先向我解释一下为什么会发生这种情况,然后我该如何解决它?

作为一个天真的 JavaScript 开发人员,我尝试将 google 脚本包含在我的 head 标签中。然后产生了类似这个问题的内容( $ 未定义)除了我们不从 google 加载 jQuery,我们在本地托管它。

我们通过这种方式成功加载了许多其他内联 jQuery 扩展以及 jQuery 代码的额外部分,所以在我看来这应该可行。我不知道 jQuery 是否妨碍了 Google,反之亦然,但他们不应该这样做。

想法?

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:

Google is not defined error message screenshot.

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:

So google does actually exist. This picture proves it.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

悲凉≈ 2024-11-06 10:52:54

删除 $(document).ready 应该可以解决您的问题。我尝试了你的代码,在注释掉 $(document).ready 后,它起作用了:

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

现在至于为什么会出现这种情况,我不知道......无论如何,你不应该需要等待 document.ready调用 google.load; google.load 将确保在调用回调drawChart 时,它可以安全执行。

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.

我一直都在从未离去 2024-11-06 10:52:54

您可以在页面加载后加载图表,但使用特殊的回调。

google.load(“可视化”,“1”,{“回调”:drawChart});

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文