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";开发者_StackOverflow中文版
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...
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.
- 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. - 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 aDate
which when alerted reports"Invalid Date"
. Evennew Date(null)
is ok. - 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?
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?
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
.
精彩评论