We help IT Professionals succeed at work.
Get Started

Showing HTML within an iFrame

ambuli
ambuli asked
on
171 Views
Last Modified: 2015-06-09
Hi there,
I want to print a pie chart inside an iFrame.  I have the following code.  When executed,  I am getting an exception saying
"Cannot read property 'getContext' of null"

I am using Charts.js library for creating the pie chart.
Can you please help me with this one.  Thank you.


// in a function I have the following code segment
if(response.success)
{
    generateReport();
}
And I just want to display the pie chart in the iFrame.

function __pieChartExample() {
        var div = document.createElement("div");
        var canvasDiv = document.createElement("canvas");
        canvasDiv.id = "chartArea";
        div.appendChild(canvasDiv);
        var ctx = document.getElementById("chartArea").getContext("2d");

        var data = [{
            value : 300,
            color : "#F7464A",
            highlight : "#FF5A5E",
            label : "Red"
        }, {
            value : 50,
            color : "#46BFBD",
            highlight : "#5AD3D1",
            label : "Green"
        }, {
            value : 100,
            color : "#FDB45C",
            highlight : "#FFC870",
            label : "Yellow"
        }];

        var pieOptions = {
            segmentShowStroke : false,
            animateScale : true
        };
        new Chart(ctx).Doughnut(data, pieOptions);
        div.appendChild(canvasDiv);
        return div;

    }
 function __generateReport() {
        var iFrame = document.createElement("iframe");
        iFrame.id = "iFrameID";
        iFrame.style.display = "none";
        document.body.appendChild(iFrame);
        iFrame.contentWindow.document.write(pieChartExample().innerHTML);
        var result = iFrame.contentWindow.document.execCommand("save", false, null);
        if (result === false) {
            iFrame.contentWindow.focus();
            iFrame.contentWindow.print();
        }
    }

Open in new window

Comment
Watch Question
This problem has been solved!
Unlock 1 Answer and 2 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE