简体   繁体   中英

How can I know if Google Visualization is loaded

I'm showing Google's charts in some web pages. But I can't assure my clients will have network access to Google: a client computer will be in the same LAN as my web server (which can access Google), but I'm not guaranteed all the clients will have access outside the LAN.

I'd like to show data using Google Charts to those clients who can access it, and a plain HTML table to those who don't.

I tried setting a variable to false, and changing it to true in a method called when the Google Visualization API is loaded:

var canAccessGoogleVisualizationVar = false;
google.load('visualization', '1', {packages: ['corechart'], callback: canAccessGoogleVisualization});
function canAccessGoogleVisualization() 
{
    canAccessGoogleVisualizationVar = true;
}

But it doesn't seem to work.

How can I know from the client side if the Google Visualization is accesible or not?


Update: The code above didn't work because of the following code (which I didn't post before as I thought wasn't meaningful):

google.setOnLoadCallback(drawVisualization);

function drawVisualization() 
{
    // Check if Google Visualization is loaded
    if (!canAccessGoogleVisualizationVar) {
        alert('Can't access Google Visualization');
    }

    // The following code can be any of the samples from Google (see http://code.google.com/apis/ajax/playground/?type=visualization#pie_chart).
    var data = new google.visualization.DataTable();
    // Add columns and values to data
    ...
    // Call new google.visualization.AnyChartBuilderFromTheAPI(<element>).draw(data);
}

I noticed my code didn't work because, if canAccessGoogleVisualizationVar == true , the if branch isn't taken, and if its false , the function drawVisualization() wouldn't have been executed.

So I took the if-test outside the function:

google.setOnLoadCallback(drawVisualization);

function drawVisualization() 
{
    // Any drawVisualization unchanged from the samples from Google (see http://code.google.com/apis/ajax/playground/?type=visualization#pie_chart).
}

// Check if Google Visualization is loaded at the end of this <script> </script>
if (!canAccessGoogleVisualizationVar) {
    alert('Can't access Google Visualization');
}
</script>

But now it's not working because the evaluation if (!canAccessGoogleVisualizationVar) is being executed before the line google.load(?, ?, canAccessGoogleVisualization); calls the method canAccessGoogleVisualization() .

How can I be sure I'm reading the value of canAccessGoogleVisualizationVar after having tried to execute the call to google.load(...); ?

You can try

function canAccessGoogleVisualization() 
{
    if ((typeof google === 'undefined') || (typeof google.visualization === 'undefined')) {
       return false;
    }
    else{
     return true;
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM