...

View Full Version : A beginner needs some help



petergr
03-21-2004, 03:24 PM
Hi, I'm javascript beginner and I need some help. Can anybody translate these functions in English?
I can't find similar examples in books or in the Internet.
code
function KeyPress(e,nf) {
kk=e.which;
kk ? kk=kk:kk=event.keyCode;
if (kk==13) {eval("document.forms[0]."+nf+".focus()");e.returnValue=false};
};
function KeyPress1(e) {
kk=e.which;
kk ? kk=kk:kk=event.keyCode;
if (kk==13) {fsubmit('login');e.returnValue=false};
};
/code
Thank you......

Antoniohawk
03-22-2004, 01:29 AM
function KeyPress(e,nf) { //function "KeyPress" accepting two arguments, "e" and "nf"
kk = e.which; //variable "kk" is equal to the charCode of the key "e"
kk ? kk=kk:kk=event.keyCode; //looks like a messed up conditional statement
if (kk==13) {
eval("document.forms[0]."+nf+".focus()"); //sets focus to the the whatever field "nf" is
e.returnValue = false; //suppresses the event that the browser has programmed for this key
}
}

function KeyPress1(e) {
kk=e.which;
kk ? kk=kk:kk=event.keyCode;
if (kk==13) {
fsubmit('login');
e.returnValue = false;
}
}

I didn't comment the second function because it pretty much deals with the same sort of things. Just so you know, I looked up all of comments I just gave you on the web. Google is my buddy! :D

petergr
03-22-2004, 01:48 AM
Thank you, Antoniohawk
But what the line code kk = e.which /code means?
What does the word "which" means? I didn't find this word among reserved of javaScript.
And again, what does the line code kk = event.keyCode /code means? Thank you.....

Antoniohawk
03-22-2004, 01:56 AM
I looked for that for practically a half an hour and finally found this site:
http://www.dansteinman.com/dynduo/en/keystrokes.html

petergr
03-22-2004, 03:28 AM
Thanks a lot for your time, Antoniohawk.
I was looking for similar information much longer then you with no success.
Thanks again…..

whammy
03-22-2004, 04:16 AM
Hehe... by the way that "messed up conditional statement" can come in handy.

It's basically a shortcut for an if/then/else statement, called a "ternary operator".

Here's an example I wrote to determine if someone is pressing a number key, backspace, enter, or delete:



<html>
<head>
<title>isNumericKey() Test</title>
<script type="text/javascript">
<!--
function isNumericKey(e)
{
var k = document.all ? e.keyCode : e.which;
return ((k > 47 && k < 58) || k == 8 || k == 0 || k == 13);
}
// -->
</script>
</head>
<body>
<form id="form1" action="javascript://" method="post">
<input type="text" name="txtField1" value="" onkeypress="return isNumericKey(event)" />
</form>
</body>
</html>


http://www.mredkj.com/tutorials/reference_js_ternary.html
http://cplus.about.com/library/glossary/bldef-ternaryoperator.htm
http://www.devdaily.com/java/edu/pj/pj010018/

petergr
03-22-2004, 04:22 AM
So there is nothing "messed up" about this line?

whammy
03-22-2004, 04:24 AM
Nope, as you can see from the links I posted in my edited post above, many languages use the same syntax as a one-line shortcut for an if/else statement. :)

I should mention that I agree with Antonio Hawk - Google is my buddy too. :p

It should be any programmer's buddy. I've had to program in languages I've never dealt with before, and have no books on (i.e. JSP), and Google can really be a life saver in those situations. :D

petergr
03-22-2004, 04:30 AM
Thank you Whammy.
As far as I understand. there are different ways in which ME and Netscape handle keystroke.... Is it a common practice (I'm new to javaScript) for this approach in coding : If this is ME do this piece of code else do that?

whammy
03-22-2004, 04:34 AM
Yeah. It is. I'm assuming by "ME" you mean Internet Explorer.

That's what the

document.all ? dothis : dothat;

does. The main problem is that you have browsers like Internet Explorer which have proprietary coding (they don't follow standards recommendations, etc.).

This situation has actually improved in the last couple of years due to a lot of browsers coming out that have great standards support, for instance Mozilla and Mozilla FireFox.

http://www.mozilla.org

After using Mozilla Firefox (even regular Mozilla), I only use Internet Explorer to test my web pages for IE users. Mozilla is by far the best browser out there, and I'm sure most of the experienced people on this forum would agree with me.

I personally like Mozilla Firefox even better.

petergr
03-22-2004, 04:39 AM
OK. I'll try Mozolla. And a "silly" question... What should I do so the examle code stood code: body of code - bottom line - further comments.....

whammy
03-22-2004, 04:40 AM
I'm not sure I understand what you are asking. :confused:

petergr
03-22-2004, 04:48 AM
Well, the way Antoniohawk showed my code
code:
line
body of code
bottom line

whammy
03-22-2004, 04:50 AM
Oh... click here (http://www.codingforums.com/misc.php?action=bbcode#buttons). :D


Code here

petergr
03-22-2004, 04:56 AM
Thank you, Whammy.....

whammy
03-22-2004, 04:57 AM
You're welcome!

Antoniohawk
03-22-2004, 05:10 AM
Lol, when I said that it was a messed up conditional statement, I meant that it was a form of a ternary operator that I had never seen before. Could you explain it to me perhaps, kind of pseudocode it?

whammy
03-22-2004, 05:19 AM
I will translate this function, too. It looks complicated but it's really simple.



<html>
<head>
<title>isNumericKey() Test</title>
<script type="text/javascript">
<!--
function isNumericKey(e)
{
var k = document.all ? e.keyCode : e.which;
return ((k > 47 && k < 58) || k == 8 || k == 0 || k == 13);
}
// -->
</script>
</head>
<body>
<form id="form1" action="" method="post">
<input type="text" name="txtField1" value="" onkeypress="return isNumericKey(event)" />
</form>
</body>
</html>




<!-- ignore the fact that it says "php" above, just using colors for the comments -->
<html>
<head>
<title>isNumericKey() Test</title>
<script type="text/javascript">
<!--
function isNumericKey(e)
{
/*
the parameter "e" passed above passes the keyboard event which is triggered in the input tag by the onkeypress event
*/

// is it Internet Explorer? If so, handle it , else use standard code
var k = document.all ? e.keyCode : e.which;
return ((k > 47 && k < 58) || k == 8 || k == 0 || k == 13); // return a boolean value from the function (true or false)
}
// -->
</script>
</head>
<body>
<form id="form1" action="" method="post">
<input type="text" name="txtField1" value="" onkeypress="return isNumericKey(event)" />
</form>
</body>
</html>


oops... didn't see the above post

petergr
03-22-2004, 05:19 AM
Antoniohawk, I've found this explanation:
The Conditional Operator (?:)
The conditional operator is the only ternary operator (three operands) in JavaScript and is sometimes actually called the ternary operator. This operator is sometimes written ?:, although it does not appear quite that way in code. Because this operator has three operands, the first goes before the ?, the second goes between the ? and the :, and the third goes after the :. It is used like this:


x > 0 ? x*y : -x*y

The first operand of the conditional operator must have a Boolean value--usually this is the result of a comparison expression. The second and third operands may have any value. The value returned by the conditional operator depends on the Boolean value of the first operand. If that operand is true, then the value of the conditional expression is the value of the second operand. If the first operand is false, then the value is the value of the third operand.

While you can achieve similar results using the if statement, the ?: operator is a very handy shortcut in many cases. Here is a typical usage, which checks to be sure that a variable is defined, uses it if so, and provides a default value if not.


greeting = "hello " + ((name != null) ? name : "there");

This is equivalent to, but more compact than, the following if statement:

greeting = "hello ";
if (name != null)
greeting += name;
else
greeting += "there";

whammy
03-22-2004, 05:25 AM
That is definitely an excellent example. I have links to some others in my first post in the thread.

petergr
03-22-2004, 05:30 AM
Your first post is well noted and is learned now :)

Antoniohawk
03-22-2004, 06:45 AM
I read up on ternary operators, thanks to your links and Peter's explanation. Am i correct in surmising that the following code means [ If 'k' exists then 'kk' is equal to 'kk', in the case that 'k' does not exist, 'kk' is equal to event.keyCode ]?


kk ? kk = kk : kk = event.keyCode;

whammy
03-22-2004, 07:44 AM
Actually, they are taking two lines instead of one, and using the exact reverse approach from what I did... both ways would work though.

The only thing you weren't right about is this:

kk = e.which; //variable "kk" is equal to the charCode of the key "e"
kk ? kk=kk:kk=event.keyCode; //looks like a messed up conditional statement


...this says if kk exists, then it's assigned back to itself, else, it's assigned to event.keyCode (using equals is not appropriate in this case as a single equals sign is an assignment operator). This is after the line before, which checks to see if e.which exists.

So basically it's checking to see if it's NOT IE, then if it isn't NOT IE, use IE's keyCode.

Antoniohawk
03-22-2004, 08:19 PM
I finally understand, thank you Whammy.

whammy
03-22-2004, 08:30 PM
I think the way I did it makes more sense... to check for a condition, rather than to check that it's not a not condition. :p

Antoniohawk
03-22-2004, 08:50 PM
You've got that right, reversing it just adds to confusion and illegibility.

shlagish
03-23-2004, 03:12 AM
Another way to do it (which I find less confusing) would be:

replace:
kk ? kk=kk : kk=event.keyCode;
by:
kk= (kk) ? kk : event.keyCode;
It's even shorter...

whammy
03-23-2004, 03:37 AM
If you replace the above with:



var k = document.all ? e.keyCode : e.which;


Not only are you using one letter (k) instead of (kk), but you're also eliminating the entire line that assigns kk to e.which. :p

P.S. To me the conditional operator is basically useless in the case of "kk = kk" since in this case the code is assigning "kk"'s value back to itself. That's the kind of programming you DON'T want to do, since it's just a waste, IMHO.

Like I said, I believe the other approach to be backwards since the whole purpose of the ternary operator in this case is to see whether or not Internet Exploder is being used. Why use a double negative approach to programming in stead of a positive one? :p

It's all about being efficient. If you can do in one line what takes others 5-12 lines to program, you're learning something. It looks like the guy who wrote the original code that you're using didn't understand the usefulness or purpose of ternary operands and he was just trying them out for size, or he would have reversed his logic and kept it all in one line. :)

shlagish
03-23-2004, 04:57 AM
The best solution is indeed :

var k = document.all ? e.keyCode : e.which;

in my opinion

nice work :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum