2

I load an internal page that I developed in IE and at the bottom it displays some JS error: It says Line 107 character 6. I look at the JScript file and it has this code:

 function isLessThanStartDate(obj)
    {
     var startdate = new Date(document.getElementById('txtSD').value);
     var enddate = new Date(obj.value);
     var weekending = new Date(document.getElementById('txtWE').value);

     if (!(isDate(startdate)))
   {
        obj.style.backgroundColor="red";
        alert (obj.value + " is not a valid date!");
        obj.value="";
        return;
   }

     if (enddate < startdate)
      {
      obj.style.backgroundColor="red";
      alert ("End date: " + enddate + " cannot be less then start date: " + startdate);
      obj.value="";
      }
     else if (enddate > weekending)
     {
      obj.style.backgroundColor="red";
      alert ("End date: " + enddate + " cannot be greater then week ending date: " + weekending);
      obj.value="";
     }
      else
      {
      obj.style.backgroundColor="";
      }      
    }

Line 107 is the line where it says

var weekending = new Date(document.getElementById('txtWE').value);

Why is this complaining? I don't see anything wrong with it...

5
  • 4
    I don't believe the error report uses the phrase "some JS error". What does it actually say? Commented May 17, 2011 at 15:49
  • 3
    If it's IE, it's not much better than that... Commented May 17, 2011 at 15:51
  • @David Dorward - @Rudie is right it doesnt display much else..I tried using firebug but it doesnt give me any info (as I dont know how to use it). It says this: User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.3; .NET4.0C; .NET4.0E; .NET CLR 1.1.4322) Timestamp: Tue, 17 May 2011 15:52:00 UTC Message: Object required Line: 150 Char: 5 Code: 0 URI: localhost/blah/include/JScript.js Message: Object required Line: 107 Char: 6 Code: 0 Commented May 17, 2011 at 15:52
  • 1
    could you also supply the HTML code containing the relevant input fields (txtWE and txtSD - I presume they're input fields). My guess is that document.getElementById('txtWE').value is failing (possibly a typo in the field name or something?), but I can't be sure without seeing the HTML as well. Commented May 17, 2011 at 15:56
  • 2
    Try to set a breakpoint on the var weekending ... line and check the value of the input (and if the document.getElementById returns something at all). Commented May 17, 2011 at 15:57

3 Answers 3

5

Oftentimes, an error will tell you exactly what is wrong and where. This is the case here.

We can tell what isn't wrong on the line.

  1. First, we know we have document because we'd have larger problems if we didn't, and we'd find out two lines earlier. Scratch that.
  2. Next, we know that the value passed to the Date constructor doesn't matter. new Date("foo") will throw no error, it will just create a Date which when alerted reports "Invalid Date". Even new Date(null) is ok.
  3. Assignment can't throw this exception.

This leaves us with very little to check out. As others have suggested, document.getElementById('txtWE') must not be returning the object you're expecting. Knowing this, see this jsfiddle. If I "break" the input with id="txtWE" to be any other ID, I get your error.

Are you sure that txtWE is there?

Sign up to request clarification or add additional context in comments.

Comments

3

If your error is in IE, you can right-click on the page and View Source. Then, check THAT page for line 107. It will probably be different than that "var weending..." code. Or, if it is in fact the 107 code you posted, perhaps a Date can't be created from the txtWE value. Are you validating that txtWE input in any way?

Comments

1

From the coment on the question, the error is 'object required'. That usually means that you're trying to access a property of something that's null.

Your line 107 might do that, though, if document.getElementById('txtWE') returned null. When you run just that code, at the console, do you get an element or do you get null? To put it another way, please check whether you have an element in the page whose ID (not name!) is txtWE.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.