user2095748 user2095748 - 1 year ago 93
Javascript Question

Draw google chart in new browse window using javascript

var data = google.visualization.arrayToDataTable(r.d);
var myWindow ='', '', 'width=500,height=500,top=200,left=200');
var doc = myWindow.document;
doc.write("<body><div id='detaildata'><body>");
var table = new google.visualization.Table(doc.getElementById('detaildata'));
table.draw(data, {
title: "Bug Details",
width: 500,
height: 400,
hAxis: { title: "Daily" }

I am opening a new window from javascript. Writing a div to it. Then drawing a google chart table onto it. But table is not coming as google table. It is coming as a simple table with no styling.
How to get proper google table in the new window.

Answer Source

the new window needs access to the loader script in order to display properly

instead of opening a blank window and writing everything to it,

save a static html file, with the code you would write, already there.

example, save the following snippet to an html file,
in the same folder as the code running now

testGoogleNW.html (this will be the new window)

<!DOCTYPE html>
<html lang="en-us">
    <script src=""></script>
      google.charts.load('current', {
        callback: function () {
          var container = document.getElementById('detaildata');
          var table = new google.visualization.Table(container);

          // use data from opening page
          table.draw(, {
            title: 'Details'
        packages: ['table']
      div {
        padding: 6px 6px 6px 6px;
    <div id="detaildata"></div>

calling window
in the code you have now, use the following snippet to prepare the data and open the window

once the window is opened, it will take over from there

data here needs to be available via global scope,
the new window uses window.opener to get the data from this page

var data;

google.charts.load('current', {
  callback: drawChart,
  packages: ['table']

function drawChart() {
  data = google.visualization.arrayToDataTable([
    ['Year', 'Sales', 'Expenses', 'Profit'],
    ['2013', 1000, 400, 200],
    ['2014', 1170, 460, 250],
    ['2015', 660, 1120, 300],
    ['2016', 1030, 540, 350]

  // open static html file'testGoogleNW.html', '', 'width=500,height=500,top=200,left=200');

you could also use localStorage to pass the variable

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download