Friday, May 9, 2008

The Difference Between onBlur and onChange Event Handlers

There's not a lot of difference between the onChange and onBlur commands, but they both have their uses.

The onBlur event is triggered when the focus leaves the field which normally means that the user has pressed the tab key to move to the next field or used the mouse to click on another field. The onChange event does almost the same thing.

The difference is that if the data did not change, onBlur would still be called anyway but onChange would not. The first time the user enters this field the difference is moot. But if the cursor passes through the field again without the user changing the data then the event will be triggered again and an unnecessary call will be made to the server. So the most efficient event to use would be onChange. We need to create a function that will handle the onChange event and we need to call that function when the event occurs.

Here is some list of Event Handlers of Javascript:
* onAbort - The user aborts the loading of an image
* onBlur - form element loses focus or when a window or frame loses focus.
* onChange - select, text, or textarea field loses focus and its value has been modified.
* onClick - object on a form is clicked.
* onDblClick - user double-clicks a form element or a link.
* onDragDrop - user drops an object (e.g. file) onto the browser window.
* onError - loading of a document or image causes an error.
* onFocus - window, frame, frameset or form element receives focus.
* onKeyDown - user depresses a key.
* onKeyPress - user presses or holds down a key.
* onKeyUp - user releases a key.
* onLoad - browser finishes loading a window or all of the frames within a frameset.
* onMouseDown - user depresses a mouse button.
* onMouseMove - user moves the cursor.
* onMouseOut - cursor leaves an area or link.
* onMouseOver - cursor moves over an object or area.
* onMouseUp - user releases a mouse button.
* onMove - user or script moves a window or frame.
* onReset - user resets a form.
* onResize - user or script resizes a window or frame.
* onSelect - user selects some of the text within a text or textarea field.
* onSubmit - user submits a form.
* onUnload - user exits a document.

Thursday, May 8, 2008

The Difference between null and undefined

JavaScript is different from other programming languages by containin both undefined and null values, which may cause confusion.

null is a special value meaning "no value". null is a special object because typeof null returns 'object'.

On the other hand, undefined means that the variable has not been declared, or has not been given a value.

Following snippets display 'undefined':

// i is not declared anywhere in code
alert(typeof i);

var i;
alert(typeof i);

Although null and undefined are slightly different, the == (equality) operator considers them equal, but the === (identity) operator doesn't.