我正在一些网页中展示谷歌图表。但我不能保证我的客户能够访问谷歌:客户端计算机将与我的Web服务器在同一个局域网中(可以访问谷歌),但我不能保证所有客户端都能够访问局域网之外的网络。
我想向那些能够访问谷歌的客户展示数据,并向那些不能访问的客户展示普通的HTML表格。
我尝试将一个变量设置为false,并在Google Visualization API加载时调用的方法中将其更改为true:
我注意到我的代码没有起作用,因为如果
所以我将if测试移到函数外面:
但现在它不起作用了,因为评估
我如何确保在尝试执行调用
我想向那些能够访问谷歌的客户展示数据,并向那些不能访问的客户展示普通的HTML表格。
我尝试将一个变量设置为false,并在Google Visualization API加载时调用的方法中将其更改为true:
var canAccessGoogleVisualizationVar = false;
google.load('visualization', '1', {packages: ['corechart'], callback: canAccessGoogleVisualization});
function canAccessGoogleVisualization()
{
canAccessGoogleVisualizationVar = true;
}
但它似乎不起作用。
如何从客户端知道Google可视化是否可访问?
更新:上面的代码没有起作用,因为以下代码(我之前没有发布,因为我认为它没有意义):
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);
}
我注意到我的代码没有起作用,因为如果
canAccessGoogleVisualizationVar == true
,则不会执行if
分支,如果它是false
,则function drawVisualization()
也不会被执行。所以我将if测试移到函数外面:
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>
但现在它不起作用了,因为评估
if (!canAccessGoogleVisualizationVar)
在调用方法canAccessGoogleVisualization()
的行google.load(?, ?, canAccessGoogleVisualization);
之前被执行。我如何确保在尝试执行调用
google.load(...);
后读取canAccessGoogleVisualizationVar
的值?