473,699 Members | 2,628 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Events

Hi everyone
I am currently confused about a piece of code that I have encountered:

Expand|Select|Wrap|Line Numbers
  1.  
  2. function handleKeyUp(e){
  3.  
  4. e = (!e) ? window.event : e; //get the event
  5. target = (!e.target) ? e.srcElement : e.target; //get the event's
  6. target
  7. if (target.nodeType == 3)
  8. target = target.parentNode;
  9.  
  10. if (e.charCode)
  11. code = e.charCode;
  12. else
  13. if (e.keyCode)
  14. code = e.keyCode;
  15. else
  16. if (e.which)
  17. code = e.which;
  18. else
  19. code = 0;
  20.  
  21. if (e.type == "keyup") {
  22. isKeyUpDownPressed = false;
  23.  
1) Is e here an event?
2) what's the purpose of e = (!e) ? window.event : e; //get the event
3) What's if (target.nodeTyp e == 3)
target = target.parentNo de;'
4) What does this piece of code do?
Nov 11 '08 #1
11 1448
disappearedng meinte:
Hi everyone
I am currently confused about a piece of code that I have encountered:

Expand|Select|Wrap|Line Numbers
  1. function handleKeyUp(e){
  2.     e = (!e) ? window.event : e; //get the event
Expand|Select|Wrap|Line Numbers
  1.  
  2. e = e || window.event;
  3.  
  4.         
  5.                     target = (!e.target) ? e.srcElement : e.target; //get the event's
  6.  
  7. target = e.target || e.srcElement;
  8.  
  9.         
  10.                 target
  11.     if (target.nodeType == 3)
  12.         target = target.parentNode;
  13.     if (e.charCode)
  14.         code = e.charCode;
  15.     else
  16.         if (e.keyCode)
  17.             code = e.keyCode;
  18.         else
  19.             if (e.which)
  20.                 code = e.which;
  21.             else
  22.                 code = 0;
  23.     if (e.type == "keyup") {
  24.         isKeyUpDownPressed = false;
  25.  
  26.  
>
1) Is e here an event?
2) what's the purpose of e = (!e) ? window.event : e; //get the event
e = e || window.event; will suffice.
If e is falsy (e.g. undefined), e will get a reference to window.event.
A necessity due to different event models in the various browser DOMs.
3) What's if (target.nodeTyp e == 3)
target = target.parentNo de;'
What it says: If the nodeType of the element that received the event is
3 (a text node), target will be re-assigned to the parentNode (normally
an element node).
4) What does this piece of code do?
A keyup listener, that tries to assign event target and key code to
global variables. And then another variable isKeyUpDownPres sed is set to
false.
That's it. Pretty bad code and style.

Gregor
Nov 11 '08 #2
Gregor Kofler wrote:
disappearedng meinte:
>I am currently confused about a piece of code that I have encountered:

[code]
function handleKeyUp(e){

e = (!e) ? window.event : e; //get the event

e = e || window.event;
if (!e)
{
e = (typeof window != "undefined"
&& typeof window.event != "undefined" )
? window.event
: null;
}

if (e)
{
// ...
}
> target = (!e.target) ? e.srcElement : e.target; //get the event's

target = e.target || e.srcElement;
*and*

if (target)
{
// ...
}
e = e || window.event; will suffice.
It won't.
If e is falsy (e.g. undefined), e will get a reference to window.event.
Iff type-converting that value to boolean does not throw an exception.
A necessity due to different event models in the various browser DOMs.
A beginning for handling that.
PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann
Nov 11 '08 #3
On Nov 11, 8:07 am, disappearedng <disappeare...@ gmail.comwrote:
function handleKeyUp(e){
e = (!e) ? window.event : e; //get the event
target = (!e.target) ? e.srcElement : e.target; //get the event's

1) Is e here an event?
2) what's the purpose of e = (!e) ? window.event : e; //get the event
e here is the event object. Regular browsers will pass the event
object as the first argument to the event handler. Hence the
'handleKeyUp(e) ' bit of code. But IE does it differently. It doesn't
pass anything to the event handler (which makes e undefined) instead
IE implements the event object as a global variable. Hence the
'window.event' thing. So, the code is checking to see if e is
'falsy' (and undefined IS falsy) and if it is, assign window.event to
e, otherwise assign e back to itself. Alternative ways to write this
are:

/* if e is falsy then e is window.event */
if (!e) {
e = window.event;
}

or my preferred way to write this

/* e is e or window.event */
e = e || window.event;

or some may even prefer

/* if e is undefined then e is window.event */
if (e == undefined) {
e = window.event;
}
3) What's if (target.nodeTyp e == 3)
target = target.parentNo de;'
Some browsers, particularly Webkit based browsers like Safari and
Nokia's web browser for the S60 platform sometimes triggers the event
on the text node rather than the node containing the text node. This
is not typically what you'd want. What you'd typically want is to know
which <divor <spanor <petc. is associated with the event. So if
we get a text node (nodeType 3) the real node we're interested in
would be its parent node.
Nov 11 '08 #4
slebetman wrote:
On Nov 11, 8:07 am, disappearedng <disappeare...@ gmail.comwrote:
>function handleKeyUp(e){
e = (!e) ? window.event : e; //get the event
target = (!e.target) ? e.srcElement : e.target; //get the event's

1) Is e here an event?
2) what's the purpose of e = (!e) ? window.event : e; //get the event

e here is the event object. Regular browsers will pass the event object
as the first argument to the event handler.
s/Regular/Standards-compliant/
Hence the 'handleKeyUp(e) ' bit of code. But IE does it differently. It
doesn't pass anything to the event handler (which makes e undefined)
instead IE implements the event object as a global variable.
No, it makes `event' a *property* of a Window object, which is in the scope
chain, to store a reference to an Event object.
PointedEars
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee
Nov 11 '08 #5
On Nov 11, 12:12*am, Thomas 'PointedEars' Lahn <PointedE...@we b.de>
wrote:
slebetman wrote:
On Nov 11, 8:07 am, disappearedng <disappeare...@ gmail.comwrote:
function handleKeyUp(e){
* * * * e = (!e) ? window.event : e; //get the event
* * * * target = (!e.target) ? e.srcElement : e.target; //get the event's
1) Is e here an event?
2) what's the purpose of * * * *e = (!e) ? window.event : e;//get the event
e here is the event object. Regular browsers will pass the event object
as the first argument to the event handler.

s/Regular/Standards-compliant/
Hence the 'handleKeyUp(e) ' bit of code. But IE does it differently. It
doesn't pass anything to the event handler (which makes e undefined)
instead IE implements the event object as a global variable.

No, it makes `event' a *property* of a Window object, which is in the scope
chain, to store a reference to an Event object.
It's probably a very obvious answer, but you can guess that I'm new at
this.

I don't know if I'm missing the point here. Saying that it isn't a
global var but instead a property of the window object is 'only' a
conceptual mistake, right? I mean, the window's properties behave
pretty much like global vars, don't they?
PointedEars
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee
Cheers

Gabriel Gilini
Nov 11 '08 #6
Thomas 'PointedEars' Lahn <Po*********@we b.dewrites:
Gregor Kofler wrote:
>disappearedn g meinte:
>>I am currently confused about a piece of code that I have encountered:

[code]
function handleKeyUp(e){

e = (!e) ? window.event : e; //get the event

e = e || window.event;

if (!e)
{
e = (typeof window != "undefined"
&& typeof window.event != "undefined" )
? window.event
: null;
}
Why bother?
If window doesn't exist, then I'd be willing to let the code fail
visibly. Something is so screwed up anyway, that I see no reason
to fail gracefully.
If window.event doesn't exist, then
e = e || window.event;
will assign undefined to e, which is just as good as null.
if (e)
{
// ...
}
Again, if the above doesn't give us an event, then we are in a
non-standard compliant setting that we haven't ever heard of before.
Anybody creating a new non-standard compliant and non-IE compatible
setting deserves to see crashes.
>e = e || window.event; will suffice.

It won't.
For practical use, I'd say it would. Can you give a non-theoretical
example where it would fail?
>If e is falsy (e.g. undefined), e will get a reference to window.event.

Iff type-converting that value to boolean does not throw an exception.
That is possible. After all, it's a host object, so it can do anything.

/L
--
Lasse Reichstein Holst Nielsen
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Nov 11 '08 #7
Lasse Reichstein Nielsen wrote:
Thomas 'PointedEars' Lahn <Po*********@we b.dewrites:
>Gregor Kofler wrote:
>>disappeared ng meinte:
I am currently confused about a piece of code that I have encountered:

[code]
function handleKeyUp(e){

e = (!e) ? window.event : e; //get the event
e = e || window.event;
if (!e)
{
e = (typeof window != "undefined"
&& typeof window.event != "undefined" )
? window.event
: null;
}

Why bother?
Because it just isn't a two-browser or two-DOM world.
If window doesn't exist, then I'd be willing to let the code fail
visibly. Something is so screwed up anyway, that I see no reason
to fail gracefully.
If window.event doesn't exist, then
e = e || window.event;
will assign undefined to e, which is just as good as null.
From the event object reference not being passed as `e' does not follow that
`window.event' is viable.
> if (e)
{
// ...
}

Again, if the above doesn't give us an event, then we are in a
non-standard compliant setting that we haven't ever heard of before.
Anybody creating a new non-standard compliant and non-IE compatible
setting deserves to see crashes.
It is a really Bad Idea to try blaming users for the mistakes of the vendors
of their software. It is one thing not to support something; it is another
to break it willingly.
>>e = e || window.event; will suffice.
It won't.

For practical use, I'd say it would. Can you give a non-theoretical
example where it would fail?
No, but I rather program defensively now instead of having to deal with code
that breaks something somewhere later. Having bugs reported (which can also
be done in an automated way) is an entirely different animal than actually
embracing them.
PointedEars
--
var bugRiddenCrashP ronePieceOfJunk = (
navigator.userA gent.indexOf('M SIE 5') != -1
&& navigator.userA gent.indexOf('M ac') != -1
) // Plone, register_functi on.js:16
Nov 11 '08 #8
Gabriel Gilini wrote:
Thomas 'PointedEars' Lahn wrote:
>slebetman wrote:
>>On Nov 11, 8:07 am, disappearedng <disappeare...@ gmail.comwrote:
Hence the 'handleKeyUp(e) ' bit of code. But IE does it differently.
It doesn't pass anything to the event handler (which makes e
undefined) instead IE implements the event object as a global
variable.
No, it makes `event' a *property* of a Window object, which is in the
scope chain, to store a reference to an Event object.

[...] I don't know if I'm missing the point here. Saying that it isn't a
global var but instead a property of the window object is 'only' a
conceptual mistake, right?
Such theoretical misconceptions can lead to very practical problems.
I/We have seen and discussed this numerous times before.
I mean, the window's properties behave pretty much like global vars,
don't they?
They don't. For one thing, provided the Window host object can be augmented
with user-defined properties (which should not be assumed), such properties
can be deleted. Global variables, as properties of the Global Object that
have the DontDelete attribute, can't.

This also isn't really news, and can (and should) readily be researched
before asking about it here again.

Please trim your quotes as observed here, and as described in the FAQ.

<http://jibbering.com/faq/#posting>
PointedEars
--
var bugRiddenCrashP ronePieceOfJunk = (
navigator.userA gent.indexOf('M SIE 5') != -1
&& navigator.userA gent.indexOf('M ac') != -1
) // Plone, register_functi on.js:16
Nov 11 '08 #9
On 11 nov, 17:47, Thomas 'PointedEars' Lahn <PointedE...@we b.de>
wrote:
I mean, the window's properties behave pretty much like global vars,
don't they?

They don't. *For one thing, provided the Window host object can be augmented
with user-defined properties (which should not be assumed), such properties
can be deleted. *Global variables, as properties of the Global Object that
have the DontDelete attribute, can't.
I see. Thank you for that.
This also isn't really news, and can (and should) readily be researched
before asking about it here again.

Please trim your quotes as observed here, and as described in the FAQ.

<http://jibbering.com/faq/#posting>
And sorry about this.

Cheers

Gabriel Gilini
Nov 11 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2035
by: Sasha | last post by:
Hi everyone, Here is my problem: I have the following classes: - DataNode - this class is designed to hold some data and will be contained in a tree like data structure DataTree. When DataNode is changed, it raises "Changed" event. The class has a reference to the DataNode it is being contained in: - DataTree - tree like data structure that contains DataNodes; When
6
13239
by: Saso Zagoranski | last post by:
Hi! How can I unregister all the events registered to a control? I have seen a piece of code in another thread, which gets all the registered handlers for a specific event. Let's say I have a CustomTextBox : TextBox, which has quite a lot of events... Do I put unregistering code for all the possible events in the Dispose method? Or is there another way?
14
12132
by: JPRoot | last post by:
Hi I use the following syntax to have events inherited from base to child classes which works nicely (virtual and override keyword on events). But I am wondering if it is a "supported" way of using events since I never saw it used anywhere in MSDN documentation/samples?! Or it will just break when I upgrade to .NET Framework 2.x in the coming years namespace MyNamespac public delegate void MyDel() public class MyBase public virtual...
2
1850
by: Bob Rundle | last post by:
I have the following code, which appears to be working. However it doesn't look right. The part I am wondering about is the logic in DisconnectEvents(). This logic creates a new delegate and then expects to find that delegate in the list of event handlers. What is the proper way of writing this? Regards, Bob Rundle
4
22879
by: LP | last post by:
Hello! I am still transitioning from VB.NET to C#. I undertand the basic concepts of Delegates, more so of Events and somewhat understand AsyncCallback methods. But I need some clarification on when to use one over another? If anyone could provide any additional info, your comments, best practices, any good articles, specific examples, etc. Thank you
11
1869
by: Nicky Smith | last post by:
Hello, I'm studying a book on VB.net Win apps, and I'm reading a section on events and delegates and raising events. Is it just me, or is this not just subs dressed up as something else? I mean, for one, delegates point to subs, so when you call a delegate, why not just call the sub dierectly and not bother adding the extra code involved adding the delegate?
30
3638
by: Burkhard | last post by:
Hi, I am new to C# (with long year experience in C++) and I am a bit confused by the language construct of events. What is it I can do with events that I cannot do with delegates? At the moment it seems to me that Microsoft has developed similar functionality via two keywords. I do understand that an event offers better encapsulation as the underlying delegate is private, but is that all ? -- Regards
5
4378
by: Richard Maher | last post by:
Hi, Here I mean "User" in the Programmer or Javascript sense. I merely wish to programmatically trigger an Event. It would be absolutely fantastic if there was a (Form level?) ONUSEREVENT() and a setEvent() function but, in the absence of this, is it not possible to use DOM to change an object's properties resulting in a state-change event? I have tried with ONCHANGE() and that only seems to work if the "interactive" user changes the...
14
2211
by: xoozlez | last post by:
Hi there, I have a registration form where I like to filter out the past events of 2007. This is the code I am using : strSQL = "SELECT EventID, EventName, EventDateBegin, EventDateEnd, EventTimeBegin, Category FROM Events WHERE EventDateBegin >'01/01/2008' AND Events.Category LIKE '3' OR Events.Category LIKE '4' OR Events.Category LIKE '5' OR Events.Category LIKE '6' OR Events.Category LIKE '7' OR Events.Category LIKE '8' OR...
1
4903
by: swethak | last post by:
Hi, I am desiging the calendar application for that purpose i used the below code. But it is for only displys calendar. And also i want to add the events to calendar. In that code displys the events when click on that date that perticular event displyed in a text box.But my requirement is to when click on that date that related event displyed in same td row not the text box. and also i add the events to that calendar.plz advice how to...
0
8704
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9192
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9054
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8940
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8895
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7781
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4390
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3071
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2362
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.