Click here to Skip to main content
13,298,731 members (47,269 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I am developing a html5 image viewer in testing the script with .html page it was working fine.when using the script in .aspx page it throws me the following error

error:0x800a01b6 - Microsoft JScript runtime error: Object doesn't support property or method 'getContext'
the bold line throws the above error

         function loadImages(sources, callback) {
             var images = {};
             var loadedImages = 0;
             var numImages = 0;
             // get num of sources
             for (var src in sources) {
             for (var src in sources) {
                 images[src] = new Image();
                 images[src].onload = function () {
                     if (++loadedImages >= numImages) {
                 images[src].src = sources[src];
         var canvas = document.getElementById('myCanvas');
         var context = canvas.getContext('2d');
         var sources = {
             web1: 'http://localhost:2861/images/1.jpg',
             web2: 'http://localhost:2861/images/2.jpg'
         loadImages(sources, function (images) {
             context.drawImage(images.web1, 100, 35, 256, 256);
             context.drawImage(images.web2, 350, 55, 93, 104);
Posted 2-Jan-13 3:45am
Updated 2-Jan-13 3:46am
Adam R Harris 2-Jan-13 10:18am
Sounds like your script is running on the sever side of things. Where is this script located?
What do you mean? Server-side JavaScript? Why would you think so? I think this thing is quite rare...
Adam R Harris 2-Jan-13 17:57pm
Like if he set the runat of his script to be server it would run the JS on the server.
I was doing some classic ASP/HTML5 stuff a little while ago and a fellow dev who wasn't to familiar with server script vs client script got pretty much the same error when his JS was running on the server.

However 2 cups of coffee and a few hours later it sounds like this isnt his issue and that his issue has something to do with his canvas not being on the page or rendered by the time this script runs.
Are you going to provide OP some help?
Adam R Harris 2-Jan-13 20:14pm
Christian is on the job, if need be ill jump in there.
RAGUNATH V 3-Jan-13 0:31am
am actually converting a .dcm image to common graphics format by using code on the fly(on the server side) .i need to display the converted file in the canvas element.
but now am storing the images in images folder in png format and i tried to display in actual need is not to store the converted images anywhere directly bind the bitmap to canvas element.
RAGUNATH V 3-Jan-13 0:08am
the script was placed inside the <body> tag.the script doesn't runs in server.
Adam R Harris 3-Jan-13 0:15am
Can you post the entire body of your aspx page?
RAGUNATH V 3-Jan-13 0:59am
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="viewer.aspx.cs" Inherits="dcmconversion.viewer" %>

<!DOCTYPE html >

<head runat="server">


var canvas = document.getElementById('myCanvas');
var context = canvas.getContext('2d');
var imageObj = new Image();

imageObj.onload = function() {
context.drawImage(imageObj, 69, 50);
imageObj.src = 'http://localhost:1666/images/filename.png';
<form id="form1">
<table >
<asp:Image ID="Image1" runat="server" />
<canvas width='500' height='500' id="myCanvas"></canvas></td>


Adam R Harris 3-Jan-13 9:22am
The problem is your script is running before the canvas is rendered so your object is null.
Has nothing to do with To check and see just move your script tag under your form tag. And then look at wrapping that in either the onload function or in an anonymous function.
RAGUNATH V 4-Jan-13 0:17am
Thank you.after placing the script below canvas the error cleared.
now its working fine.
Adam R Harris 4-Jan-13 12:10pm
No problem, glad i could help.

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Have you looked at the object in Chrome to see what it IS and what methods it has ? If you do that for both pages, and the object types are the same, then the only other thing I can think of is if the DTD is somehow changing the nature of your page and the objects on it.

If you paste your HTML verbatim in to an ASPX, does it still give the error ?
RAGUNATH V 3-Jan-13 0:20am
i've already tried using the html code in aspx page.the same error is thrown by visual studio.
Christian Graus 3-Jan-13 0:42am
Turn off script debugging then, perhaps that's the issue
RAGUNATH V 3-Jan-13 1:05am
Script debugging is disabled by default and also checked it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.171207.1 | Last Updated 2 Jan 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100