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

javascript - Loading Google jsapi asynchronously - Stack Overflow

programmeradmin1浏览0评论

There was an issue with Google charts today so we're trying to fail gracefully if we can't load the js file. The following works fine:

<script type="text/javascript" src="=%7B%22modules%22%3A%5B%7B%22name%22%3A%22visualization%22%2C%22version%22%3A%221.0%22%2C%22packages%22%3A%5B%22corechart%22%5D%7D%5D%7D"></script>

The problem is that it will block running other code as it is waiting for the script to time out.

The code below loads,

<script type="text/javascript">
$.ajax({
    url: '=%7B%22modules%22%3A%5B%7B%22name%22%3A%22visualization%22%2C%22version%22%3A%221.0%22%2C%22packages%22%3A%5B%22corechart%22%5D%7D%5D%7D',
    dataType: 'script',
    cache: true, // otherwise will get fresh copy every page load
    success: function() {
      google.load("visualization", "1", {packages:["corechart"]}); 
    }
});
</script>

but it errors when I try to use a visualization such as

var data = new google.visualization.DataTable();

Is what I'm doing possible or are we stuck with the problem of if Google is having issues, we just have to wait for the js file to timeout and move on?

Thanks!

There was an issue with Google charts today so we're trying to fail gracefully if we can't load the js file. The following works fine:

<script type="text/javascript" src="https://www.google./jsapi?autoload=%7B%22modules%22%3A%5B%7B%22name%22%3A%22visualization%22%2C%22version%22%3A%221.0%22%2C%22packages%22%3A%5B%22corechart%22%5D%7D%5D%7D"></script>

The problem is that it will block running other code as it is waiting for the script to time out.

The code below loads,

<script type="text/javascript">
$.ajax({
    url: 'https://www.google./jsapi?autoload=%7B%22modules%22%3A%5B%7B%22name%22%3A%22visualization%22%2C%22version%22%3A%221.0%22%2C%22packages%22%3A%5B%22corechart%22%5D%7D%5D%7D',
    dataType: 'script',
    cache: true, // otherwise will get fresh copy every page load
    success: function() {
      google.load("visualization", "1", {packages:["corechart"]}); 
    }
});
</script>

but it errors when I try to use a visualization such as

var data = new google.visualization.DataTable();

Is what I'm doing possible or are we stuck with the problem of if Google is having issues, we just have to wait for the js file to timeout and move on?

Thanks!

Share Improve this question asked Oct 1, 2013 at 21:27 Dan GetzDan Getz 552 silver badges7 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 5

Since you're calling the google.load function on success, the ?autoload=... stuff is redundant.

Just change your url to //www.google./jsapi, and add a 'callback' to the load call to make sure that your drawChart code is called when corechart pletes.

Here's a JSFiddle and a code snippet: http://jsfiddle/c56pC/2/

<script type="text/javascript">
$.ajax({
    url: '//www.google./jsapi',
    dataType: 'script',
    cache: true,
    success: function() {
        google.load('visualization', '1', {
            'packages': ['corechart'],
            'callback': drawChart
        });
    }
});
</script>

Your script won't execute once it's downloaded via AJAX. You want to use $.getScript():

$.ajaxSetup({ cache: true });
var url = 'https://www.google./jsapi?autoload=%7B%22modules%22%3A%5B%7B%22name%22%3A%22visualization%22%2C%22version%22%3A%221.0%22%2C%22packages%22%3A%5B%22corechart%22%5D%7D%5D%7D';
$.getScript(url, function() {
    google.load("visualization", "1", {packages:["corechart"]}); 
});
发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>