469,307 Members | 2,507 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,307 developers. It's quick & easy.

Object Required?

So I just started learning HTML and Javascript about a week ago, and while I'm writing this code, I get an error saying "Object Required". I don't understand why the object is required, and how to fix it, so could you please help?

Also, I probably have other errors as well, so if you could suggest any other corrections to my code, that would be great!

The error states that the object is required on line 2, character 1.

The following code is all the Javascript code I have in the head section of my HTML document so far...

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. var command=document.getElementById('tc').innerHTML;
  3. function checkCommand() {
  4.   if (command=="add todo") {
  5.     addToDo();
  6.   }
  7. }
  8. function startTime()  {
  9.   var today=new Date();
  10.   var h=today.getHours();
  11.   var m=today.getMinutes();
  12.   var s=today.getSeconds();
  13.   m=checkTime(m);
  14.   s=checkTime(s);
  15.   document.getElementById('time').innerHTML=h+":"+m+":"+s;
  16.   t=setTimeout('startTime()',500);
  17. }
  18. function checkTime(i) {
  19.   if (i<10) {
  20.     i="0" + i;
  21.   }
  22.   return i;
  23. }
  24. function myDate() {
  25.   var d=new Date();
  26.   var year=d.getFullYear();
  27.   var month=d.getMonth()+1;
  28.   var date=d.getDate();
  29.   document.getElementById('date').innerHTML=month+"/"+date+"/"+year;
  30. }
  31. function addToDo() {
  32.   var input=prompt("Type what you need To-Do...","Make it short please!");
  33.   var tdv=document.getElementById('todo').innerHTML;
  34.   if (tdv.length>0) {
  35.     tdv=tdv+"<br />"+input;
  36.   }
  37.   else {
  38.     tdv=input;
  39.   }
  40. }
  41. </script>
  42.  
Thanks!!!
May 4 '10 #1
1 1893
Dormilich
8,651 Expert Mod 8TB
you usually get an "Object required" error, when you execute code before the appropriate HTML has been built (say, if you have your script in the <head> section, line 2 will throw that error, because nothing of the <body> has been rendered yet). the way to go would be to delay any code execution until the page has completed loading, either via window.onload/window.addEventListener()/any such cross-browser event solution.

besides, that is one reason why you should avoid global variables.
May 4 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Dave Hammond | last post: by
2 posts views Thread by RICHARD BROMBERG | last post: by
1 post views Thread by Eric | last post: by
4 posts views Thread by =?Utf-8?B?cGJsYWNrYnVybg==?= | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.