Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET HTML5/CSS
I am developing a html5 image viewer in asp.net.am 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
 
 <script>
         function loadImages(sources, callback) {
             var images = {};
             var loadedImages = 0;
             var numImages = 0;
             // get num of sources
             for (var src in sources) {
                 numImages++;
             }
             for (var src in sources) {
                 images[src] = new Image();
                 images[src].onload = function () {
                     if (++loadedImages >= numImages) {
                         callback(images);
                     }
                 };
                 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);
         });
 
    </script>
Posted 2-Jan-13 2:45am
Edited 2-Jan-13 2:46am
v2
Comments
Adam R Harris at 2-Jan-13 10:18am
   
Sounds like your script is running on the sever side of things. Where is this script located?
Sergey Alexandrovich Kryukov at 2-Jan-13 17:42pm
   
What do you mean? Server-side JavaScript? Why would you think so? I think this thing is quite rare...
—SA
Adam R Harris at 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.
Sergey Alexandrovich Kryukov at 2-Jan-13 18:15pm
   
Are you going to provide OP some help?
—SA
Adam R Harris at 2-Jan-13 20:14pm
   
Christian is on the job, if need be ill jump in there.
Sergey Alexandrovich Kryukov at 2-Jan-13 20:25pm
   
Hm...
RAGUNATH V at 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 browser.my actual need is not to store the converted images anywhere directly bind the bitmap to canvas element.
RAGUNATH V at 3-Jan-13 0:08am
   
the script was placed inside the <body> tag.the script doesn't runs in server.
Adam R Harris at 3-Jan-13 0:15am
   
Can you post the entire body of your aspx page?
RAGUNATH V at 3-Jan-13 0:59am
   
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="viewer.aspx.cs" Inherits="dcmconversion.viewer" %>
 
<!DOCTYPE html >
 
<html>
<head runat="server">
<title></title>

</head>
<body>

<script>
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';
</script>
<form id="form1">
<div>
<table >
<tr>
<td>
<asp:Image ID="Image1" runat="server" />
</td></tr>
<tr><td>
<canvas width='500' height='500' id="myCanvas"></canvas></td>

</tr>
</table>

</div>
</form>
</body>
</html>
Adam R Harris at 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 asp.net. 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 at 4-Jan-13 0:17am
   
Thank you.after placing the script below canvas the error cleared.
now its working fine.
Adam R Harris at 4-Jan-13 12:10pm
   
No problem, glad i could help.

1 solution

Rate this: bad
good
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 ?
  Permalink  
Comments
RAGUNATH V at 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 at 3-Jan-13 0:42am
   
Turn off script debugging then, perhaps that's the issue
RAGUNATH V at 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
0 OriginalGriff 349
1 Nirav Prabtani 268
2 Richard Deeming 210
3 CHill60 170
4 _Amy 145
0 OriginalGriff 8,104
1 Sergey Alexandrovich Kryukov 7,067
2 Maciej Los 4,039
3 Peter Leow 3,738
4 CHill60 2,912


Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 2 Jan 2013
Copyright © CodeProject, 1999-2014
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