Jump to content

Unable to get value of the property 'innerHTML': object is null or undefined

Recommended Posts

Hi, i need help for this error, I keep getting this error -->> "Unable to get value of the property 'innerHTML': object is null or undefined" when am running the program in IE & nothing is running in other web browsers... Below is the javascript (JScript.js) code: var output = document.getElementById('output'), pressed = {}; window.onkeydown = function (e) { if (pressed[e.which]) return; pressed[e.which] = e.timeStamp;}; window.onkeyup = function (e) { if (!pressed[e.which]) return; var duration = (e.timeStamp - pressed[e.which]) / 1000; output.innerHTML += '<p>Key ' + e.which + ' was pressed for ' + duration + ' seconds</p>'; pressed[e.which] = 0;}; Here is my asp code (test.aspx): <html><head><title>XXX</title></head><body> <h1>Keystroke Dynamics</h1> <p>Try pressing some keys on your keyboard ...</p> <script type="text/javascript" src="JScript.js"></script> <div id="output"></div></body></html> May I know the problem that cause this error & how can I correct it?? I tried the same program in php and it works but not in asp.net 2010...Thank you...

Link to post
Share on other sites

the issue is most likely caused by the fact the javascript is executing before the page has finished loading, so your initial reference to the output div will not return the actual DOM element. Put that DOM method call inside a window.onload function instead (which is a best practice).

var output = {}; window.onload = function(){  output =  document.getElementById('output');};

Edited by thescientist
  • Like 1
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...