Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Events

Status
Not open for further replies.

gerases

Programmer
Apr 27, 2006
29
US
Hello,

here's a function that I have a question about:

function enterPressed(e) {
var charCode;

if (window.event) {
charCode = event.keyCode;
}
else {
charCode = e.which;
}

// alert(charCode);

if (charCode == 13) {
return true;
}
else {
return false;
}
}

Here's the question: in FF, if I say "if (event)", FF says that "event" is not defined. But "if (window.event)" works no problem. Why is that? I know that one of the ways to test whether a variable is set is to use typeof but I think the author of the Rhino book uses "if (window.event)".
 
window.event is IE-only. Try this instead:

Code:
function enterPressed(evt) {
	var charCode;

	if (evt) {
		charCode = e.which;
	} else {
		charCode = event.keyCode;
	}

	if (charCode == 13) return true;
	return false;
}

Hope this helps,
Dan

Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Yep, I know that window.event is IE only. See, I think, the problem with your version of the function is that if it's assigned within a form like so: "<INPUT type="button" onclick="enterPressed(event)">, it will work in IE because after a series of lookups in the scope chain, it will eventually get to window.event and that's what will be passed to the function. In FF, of course, "event" is a true parameter. Now, if you're in IE and "event" is defined anywhere in the scope chain before the window object, that "event" will be passed to the function.

That's why, I think, it's better to test for window.event directly than rely on the scope resolution (in IE only).

My question however was about why, in FF, when I do "if (event)", it says "event undefined" while "if (window.event)" works fine. I think FF should say "undefined" in both cases. Does it make sense?

By the same token, when I do "javascript:alert(window.event)" in the address bar of FF, it says "undefined" while "javascript:alert(event)" doesn't produce anything. What's more interesting is that both "javascript:alert(typeof event)" and "javascript:alert(typeof window.event)" give "undefined".

That's basically my question :)
 
the problem with your version of the function

Perhaps you should have clarified your requirements more, then. My code would work fine in a scenario where you didn't use HTML to assign the event handler.

Perhaps you could rework your code to do just that?

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
You're right, I should have made my question more clear.
 
Here's a thread I found that sort of discusses what I'm talking about:
A related question to my previous posts is: if "if (event)" in a function that doesn't have "event" defined produces "event undefined", then how come checking for the existence of certain methods always works? For example, when you do something like "if (obj.method1)","method1 undefined" is never produced even if "method1" IS undefined. I'm just slightly confused here...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top