Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: google.load is not a function when using ajax

I am using a Google chart (termcloud) to display some data. I can get this working fine as a static feature on the page, however when I try to load the chart and its assets via ajax it just seems to keep throwing an error:

'TypeError: google.load is not a function'

Here is my ajax function:

$("li.contentpanel").click(function() {

$("#content-panel").show();

$('#content-panel').animate({
    width: '540'
}, 500, function() {
    var dataString = 'alert=1';
    $.ajax({
    type: "POST",
    url: "<?php echo site_url($topicmaplink);?>",
    data: dataString,
    cache: false,

    success: function(html){
        $("#content-panel #inner").html(html);
    }
}); 

});

This is the page that is called:

(JSAPI and termcloud plugin files are loaded at top of this page)

$(function() {

  google.load("visualization", "1");
  google.setOnLoadCallback(draw);
  function draw() {
    data = new google.visualization.DataTable();
    data.addColumn('string', 'Label');
    data.addColumn('number', 'Value');
    data.addColumn('string', 'Link');
    data.addRows(<?php echo sizeof($topics);?>);
    <?php 
    $trans = array("ã" => "a", "³" => "3", "º" => "0", "â" => "a", 
        "¡" => ";", "'" => "", "\n" => "",'"' => '');
    shuffle($topics);
    for($j=0;$j<sizeof($topics);$j++){
      $nonforeignkeyword = strtr($topics[$j]['keyword'],$trans);
      $totalnumber = $topics[$j]['occurrence'];
      echo 'data.setValue('.$j.', 0, "'.trim($nonforeignkeyword).'");';
      echo 'data.setValue('.$j.', 1, '.$totalnumber.');';
      echo 'data.setValue('.$j.', 2, "'.$partlink.'/searchterm||'.trim(rawurlencode($nonforeignkeyword)).'");';
    }
    ?>
    var outputDiv = document.getElementById('cp-tmap');
    var tc = new TermCloud(outputDiv);
    tc.draw(data, null);
  }
});

Even when I remove the JSAPI and termcloud js files from the page being called via ajax and place them on the same page that it is being called to the page seems to redirect to google.com and hang on a blank page.

Does anyone know where I am going wrong here?

Thanks in advance for the help

like image 817
Dave Henderson Avatar asked Mar 23 '26 01:03

Dave Henderson


1 Answers

Solved this issue for anyone who may be having the same problem. Removed on the google.load and setOnLoadCallback and placed these in their own callback function after the datatable has been drawn:

<pre>
<code>
  //google.load("visualization", "1");
  //google.setOnLoadCallback(draw);
  function draw() {
    data = new google.visualization.DataTable();
    data.addColumn('string', 'Label');
    data.addColumn('number', 'Value');
    data.addColumn('string', 'Link');
    data.addRows(<?php echo sizeof($topics);?>);
    <?php 
    $trans = array("ã" => "a", "³" => "3", "º" => "0", "â" => "a", "¡" => ";", "'" => "", "\n" => "",'"' => '');
    shuffle($topics);
    for($j=0;$j<sizeof($topics);$j++){
      $nonforeignkeyword = strtr($topics[$j]['keyword'],$trans);
      $totalnumber = $topics[$j]['occurrence'];
      echo 'data.setValue('.$j.', 0, "'.trim($nonforeignkeyword).'");';
      echo 'data.setValue('.$j.', 1, '.$totalnumber.');';
      echo 'data.setValue('.$j.', 2, "'.$partlink.'/authorname||'.trim(rawurlencode($nonforeignkeyword)).'");';
    }
    ?>
     var outputDiv = document.getElementById('cp-tmap');
    var tc = new TermCloud(outputDiv);
    tc.draw(data, null);
  }


$(document).ready(function(){ 
    setTimeout(function(){ 
        google.load("visualization", "1",{"callback" : draw});  
  }, 100); 
}); 
</code>
</pre> 
like image 55
Dave Henderson Avatar answered Mar 25 '26 15:03

Dave Henderson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!