|
-
Apr 16th, 2003, 10:55 AM
#1
Thread Starter
Hyperactive Member
What does the "?" mean in this?
Hi,
In the code below, what does the question mark do in the lines var keyCode... and var filter...?
Code:
var isNN = (navigator.appName.indexOf("Netscape")!=-1);
function autoTab(input,len, e) {
var keyCode = (isNN) ? e.which : e.keyCode;
var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
if(input.value.length >= len && !containsElement(filter,keyCode)) {
input.value = input.value.slice(0, len);
input.form[(getIndex(input)+1) % input.form.length].focus();
}
Thanks,
Al.
A computer is a tool, not a toy.
-
Apr 16th, 2003, 12:29 PM
#2
I can answer part of it.... For starters, it's JAvascript -- wasn't sure you got that or not.
"var isNN" and "var keycode" and "var filter" are variable declarations.
The funk stuff after the "=" is something known as "regular expresssions".... I believe in this case that it is checking for valid data entries.
-
Apr 16th, 2003, 12:56 PM
#3
var keyCode = (isNN) ? e.which : e.keyCode;
It's the same as saying:
If (isNN) is true, then keyCOde = e.which
otherwise
if (isNN) is false, then keyCode = e.keycode
It's just a shortcut method for that
HTH
-
Apr 16th, 2003, 12:59 PM
#4
Thread Starter
Hyperactive Member
Hi,
Thanks for the reply.
I kind of understand JavaScript, e.g. var's, but I'm unfamiliar with a question mark in the variable declaration. I found something that mentioned a "switch" or "condition" but this didn't mean anything to me.
The above code came from a JavaScript AutoTab function. Here's the complete code.
Code:
<SCRIPT LANGUAGE="JavaScript">
var isNN = (navigator.appName.indexOf("Netscape")!=-1);
function autoTab(input,len, e) {
var keyCode = (isNN) ? e.which : e.keyCode;
var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
if(input.value.length >= len && !containsElement(filter,keyCode)) {
input.value = input.value.slice(0, len);
input.form[(getIndex(input)+1) % input.form.length].focus();
}
function containsElement(arr, ele) {
var found = false, index = 0;
while(!found && index < arr.length)
if(arr[index] == ele)
found = true;
else
index++;
return found;
}
function getIndex(input) {
var index = -1, i = 0, found = false;
while (i < input.form.length && index == -1)
if (input.form[i] == input)index = i;
else i++;
return index;
}
return true;
}
</script>
The HTML uses onKeyUp="return autoTab(this, 2, event); in the input box.
Thanks,
Al.
A computer is a tool, not a toy.
-
Apr 16th, 2003, 01:36 PM
#5
Thread Starter
Hyperactive Member
A computer is a tool, not a toy.
-
Apr 16th, 2003, 01:42 PM
#6
Frenzied Member
Originally posted by Al Smith
I kind of understand JavaScript, e.g. var's, but I'm unfamiliar with a question mark in the variable declaration. I found something that mentioned a "switch" or "condition" but this didn't mean anything to me.
You can declare and initialize variables in the same statement.
var keyCode = (isNN) ? e.which : e.keyCode; is just a shortcut for:
var keyCode;
keyCode = (isNN) ? e.which : e.keyCode;
isNN is the condition.
BTW, this type of statement can also be found in Java and C. It is sometimes called a ternary, most likely due to the 3 expressions that make up the entire expression: (expr1) ? expr2 : expr3.
-
Apr 18th, 2003, 02:47 AM
#7
BTW, there are no regular expressions in that code.
?: is the "conditional operator", and it is a ternary operator. This means it operates on three operands as opposed to binary (2 operands) or unary (1 operand) operators.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|