Cari di JavaScript 
    JavaScript Manual
Daftar Isi
(Sebelumnya) event, onAbort, onBlur, onChan ...onKeyDown, onKeyPress, onKeyUp ... (Berikutnya)

onDblClick

Executes JavaScript code when a DblClick event occurs; that is, when the user double-clicks a form element or a link.

Event handler for

document, Link

Implemented in

Navigator 4.0

Syntax

onDblClick="handlerText"

Parameters

handlerText
JavaScript code or a call to a JavaScript function.

Note

DblClick is not implemented on the Macintosh.

Event properties used

type
Indicates the type of event.

target
Indicates the object to which the event was originally sent.

layerX, layerY,
pageX, pageY,
screenX, screenY
Represent the cursor location at the time the event occurred.

which
Represents 1 for a left-mouse double-click and 3 for a right-mouse double-click.

modifiers
Contains the list of modifier keys held down when the event occurred.

See also

For general information on event handlers, see "General Information about Events".

For information about the event object, see event.


onDragDrop

Executes JavaScript code when a DragDrop event occurs; that is, when the user drops an object onto the browser window, such as dropping a file.

Event handler for

Window

Implemented in

Navigator 4.0

Syntax

onDragDrop="handlerText"

Parameters

handlerText
JavaScript code or a call to a JavaScript function.

Event properties used

type
Indicates the type of event.

target
Indicates the object to which the event was originally sent.

data
Returns an Array of Strings containing the URLs of the dropped objects.

modifiers
Contains the list of modifier keys held down when the event occurred.

screenX,
screenY
Represent the cursor location at the time the event occurred.

Security

Getting the data property of the DragDrop event requires the UniversalBrowserRead privilege. For information on security in Navigator 4.0, see Chapter 7, "JavaScript Security," in the JavaScript Guide.

Description

The DragDrop event is fired whenever a system item (file, shortcut, and so on) is dropped onto the browser window using the native system's drag and drop mechanism. The normal response for the browser is to attempt to load the item into the browser window. If the event handler for the DragDrop event returns true, the browser loads the item normally. If the event handler returns false, the drag and drop is canceled.

See also

For general information on event handlers, see "General Information about Events".

For information about the event object, see event.


onError

Executes JavaScript code when an error event occurs; that is, when the loading of a document or image causes an error.

Event handler for

Image, Window

Implemented in

Navigator 3.0

Syntax

onError="handlerText"

Parameters

handlerText
JavaScript code or a call to a JavaScript function.

Description

An error event occurs only when a JavaScript syntax or runtime error occurs, not when a browser error occurs. For example, if you try set window.location.href='notThere.html' and notThere.html does not exist, the resulting error message is a browser error message; therefore, onError would not intercept that message. However, an error event is triggered by a bad URL within an IMG tag or by corrupted image data.

window.onerror applies only to errors that occur in the window containing window.onerror, not in other windows.

onError can be any of the following:

  • null to suppress all JavaScript error dialogs. Setting window.onerror to null means your users won't see JavaScript errors caused by your own code.
  • The name of a function that handles errors (arguments are message text, URL, and line number of the offending line). To suppress the standard JavaScript error dialog, the function must return true. See Example 3 below.
  • A variable or property that contains null or a valid function reference.
If you write an error-handling function, you have three options for reporting errors:

  • Trace errors but let the standard JavaScript dialog report them (use an error handling function that returns false or does not return a value)
  • Report errors yourself and disable the standard error dialog (use an error handling function that returns true)
  • Turn off all error reporting (set the onError event handler to null)

Event properties used

type
Indicates the type of event.

target
Indicates the object to which the event was originally sent.

Examples

Example 1: Null event handler. In the following IMG tag, the code onError="null" suppresses error messages if errors occur when the image loads.

<IMG NAME="imageBad1" SRC="corrupt.gif" ALIGN="left" BORDER="2"
   onError="null">
Example 2: Null event handler for a window. The onError event handler for windows cannot be expressed in HTML. Therefore, you must spell it all lowercase and set it in a SCRIPT tag. The following code assigns null to the onError handler for the entire window, not just the Image object. This suppresses all JavaScript error messages, including those for the Image object.

<SCRIPT>
window.onerror=null
</SCRIPT>
<IMG NAME="imageBad1" SRC="corrupt.gif" ALIGN="left" BORDER="2">
However, if the Image object has a custom onError event handler, the handler would execute if the image had an error. This is because window.onerror=null suppresses JavaScript error messages, not onError event handlers.

<SCRIPT>
window.onerror=null
function myErrorFunc() {
   alert("The image had a nasty error.")
}
</SCRIPT>
<IMG NAME="imageBad1" SRC="corrupt.gif" ALIGN="left" BORDER="2"
   onError="myErrorFunc()">
In the following example, window.onerror=null suppresses all error reporting. Without onerror=null, the code would cause a stack overflow error because of infinite recursion.

<SCRIPT>
window.onerror = null;
function testErrorFunction() {
   testErrorFunction();
}
</SCRIPT>
<BODY onload="testErrorFunction()">
test message
</BODY>
Example 3: Error handling function. The following example defines a function, myOnError, that intercepts JavaScript errors. The function uses three arrays to store the message, URL, and line number for each error. When the user clicks the Display Error Report button, the displayErrors function opens a window and creates an error report in that window. Note that the function returns true to suppress the standard JavaScript error dialog.

<SCRIPT>
window.onerror = myOnError
msgArray = new Array()
urlArray = new Array()
lnoArray = new Array()
function myOnError(msg, url, lno) {
   msgArray[msgArray.length] = msg
   urlArray[urlArray.length] = url
   lnoArray[lnoArray.length] = lno
   return true
}
function displayErrors() {
   win2=window.open('','window2','scrollbars=yes')
   win2.document.writeln('<B>Error Report</B><P>')
   for (var i=0; i < msgArray.length; i++) {
      win2.document.writeln('<B>Error in file:</B> ' + urlArray[i] + '<BR>')
      win2.document.writeln('<B>Line number:</B> ' + lnoArray[i] + '<BR>')
      win2.document.writeln('<B>Message:</B> ' + msgArray[i] + '<P>')
   }
   win2.document.close()
}
</SCRIPT>
<BODY onload="noSuchFunction()">
<FORM>
<BR><INPUT TYPE="button" VALUE="This button has a syntax error"
   onClick="alert('unterminated string)">
<P><INPUT TYPE="button" VALUE="Display Error Report"
   onClick="displayErrors()">
</FORM>
This example produces the following output:

Error Report
Error in file: file:///c%7C/temp/onerror.html
Line number: 34
Message: unterminated string literal
Error in file: file:///c%7C/temp/onerror.html
Line number: 34
Message: missing ) after argument list
Error in file: file:///c%7C/temp/onerror.html
Line number: 30
Message: noSuchFunction is not defined
Example 4: Event handler calls a function. In the following IMG tag, onError calls the function badImage if errors occur when the image loads.

<SCRIPT>
function badImage(theImage) {
   alert('Error: ' + theImage.name + ' did not load properly.')
}
</SCRIPT>
<FORM>
<IMG NAME="imageBad2" SRC="orca.gif" ALIGN="left" BORDER="2"
   onError="badImage(this)">
</FORM>

See also

onAbort, onLoad

For general information on event handlers, see "General Information about Events".

For information about the event object, see event.


onFocus

Executes JavaScript code when a focus event occurs; that is, when a window, frame, or frameset receives focus or when a form element receives input focus.

Event handler for

Button, Checkbox, FileUpload, Layer, Password, Radio, Reset, Select, Submit, Text, Textarea, Window

Implemented in

Navigator 2.0
Navigator 3.0: event handler of Button, Checkbox, FileUpload, Frame, Password, Radio, Reset, Submit, and Window
Navigator 4.0: event handler of Layer

Syntax

onFocus="handlerText"

Parameters

handlerText
JavaScript code or a call to a JavaScript function.

Description

The focus event can result from a focus method or from the user clicking the mouse on an object or window or tabbing with the keyboard. Selecting within a field results in a select event, not a focus event. onFocus executes JavaScript code when a focus event occurs.

A frame's onFocus event handler overrides an onFocus event handler in the BODY tag of the document loaded into frame.

Note that placing an alert in an onFocus event handler results in recurrent alerts: when you press OK to dismiss the alert, the underlying window gains focus again and produces another focus event.

Note

In Navigator 3.0, on some platforms, placing an onFocus event handler in a FRAMESET tag has no effect.

Event properties used

type
Indicates the type of event.

target
Indicates the object to which the event was originally sent.

Examples

The following example uses an onFocus handler in the valueField Textarea object to call the valueCheck function.

<INPUT TYPE="textarea" VALUE="" NAME="valueField"
   onFocus="valueCheck()">
See also examples for onBlur.

See also

onBlur, onChange

For general information on event handlers, see "General Information about Events".

For information about the event object, see event.

(Sebelumnya) event, onAbort, onBlur, onChan ...onKeyDown, onKeyPress, onKeyUp ... (Berikutnya)