Javascript 如何使用google图表并设置document.domain
似乎更改document.domain属性会导致Google图表无法在Internet Explorer中工作(至少版本8)。下面是一个非常简单的页面,演示了这个问题。示例代码几乎直接来自GoogleCharts库沙箱。唯一的主要区别是我添加了一个先前的JavaScript来设置document.domain 我们正在处理一个子域,比如“test.example.com”,出于其他原因,我们需要将document.domain属性设置为“example.com”。这打破了图表。它只是说“你的浏览器不支持图表。” 即使您将document.domain设置为“test.example.com”(与默认设置完全相同),图表仍然无法工作。据我所知,Google Charts正在IE8中创建一个iframe,通过更改document.domain,您触发了同源安全策略 因为我不控制Google库,所以我无法在iframe本身上设置document.domain(因为我现在没有权限从上面的页面访问它,因为我有JavaScript)。如何在保持更改document.domain的能力的同时解决此问题Javascript 如何使用google图表并设置document.domain,javascript,iframe,google-visualization,Javascript,Iframe,Google Visualization,似乎更改document.domain属性会导致Google图表无法在Internet Explorer中工作(至少版本8)。下面是一个非常简单的页面,演示了这个问题。示例代码几乎直接来自GoogleCharts库沙箱。唯一的主要区别是我添加了一个先前的JavaScript来设置document.domain 我们正在处理一个子域,比如“test.example.com”,出于其他原因,我们需要将document.domain属性设置为“example.com”。这打破了图表。它只是说“你的浏览
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
document.domain = "example.com";
</script>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Year', 'Sales', 'Expenses'],
['2004', 1000, 400],
['2005', 1170, 460],
['2006', 660, 1120],
['2007', 1030, 540]
]);
var options = {
title: 'Company Performance',
hAxis: {title: 'Year', titleTextStyle: {color: 'red'}}
};
var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
</script>
</head>
<body>
<div id="chart_div" style="width: 900px; height: 500px;"></div>
</body>
</html>
document.domain=“example.com”;
load(“可视化”、“1”、{packages:[“corechart”]});
setOnLoadCallback(drawChart);
函数绘图图(){
var data=google.visualization.arrayToDataTable([
[“年度”、“销售额”、“费用”],
['2004', 1000, 400],
['2005', 1170, 460],
['2006', 660, 1120],
['2007', 1030, 540]
]);
变量选项={
标题:“公司业绩”,
hAxis:{title:'Year',titleTextStyle:{color:'red'}
};
var chart=new google.visualization.ColumnChart(document.getElementById('chart_div'));
图表绘制(数据、选项);
}
仍然没有解决根本原因的方法。但我们最终做的只是将document.domain属性的设置向后推,直到Google图表呈现在页面上。然后您可以设置document.domain而不会引起问题
显然,如果您要从页面动态添加/删除图表,这将不起作用,但对于绝大多数情况,它应该起作用