Showing HTML within an iFrame

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

ambuliAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Hans LangerCommented:
you have the error in:

var ctx = document.getElementById("chartArea").getContext("2d");

Open in new window


document.getElementById("chartArea") is null so you need to be sure that you have that element in your document and execute your code after the element is loaded with:

window.onload = function(){
__pieChartExample()
}

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ambuliAuthor Commented:
Thank you
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.