sasha_eddy 0 Posted March 7, 2013 Report Share Posted March 7, 2013 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... Quote Link to post Share on other sites
thescientist 231 Posted March 7, 2013 Report Share Posted March 7, 2013 (edited) 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 March 7, 2013 by thescientist 1 Quote Link to post Share on other sites
sasha_eddy 0 Posted March 10, 2013 Author Report Share Posted March 10, 2013 Now, it works... many thanks... Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.