Here is my code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>
JS Test
</title>
<script type="text/javascript" src="jquery-1.4.2.min.js"></script>
<script type="text/javascript">
if(someObj != null || typeof someObj != 'undefined')
{
alert("Success.");
}
else
{
alert("Failed.");
}
</script>
</head>
<body>
Why it always return an error message-“someObj is not defined” to me?
I want to put the codes to foot of page to check whether the someObj has been declared or not.
Do it the other way around:
That is, don’t try to actually use
someObjuntil after you are sure it has been defined as confirmed withtypeof.If in fact the variable isn’t defined, when you start your
ifcondition withsomeObj != nullyou get the error “someObj is not defined” because – well, it isn’t defined.If you first test it with
typeofthen it won’t do thenullcomparison unless it actually is defined.