dispatchEvent method
9 | ||||
Dispatches the specified event to the current element.
To create an event object use the createEvent method in Firefox, Opera, Google Chrome, Safari and Internet Explorer from version 9.
After the new event is created, initialize it first (for details, see the page for the createEvent method).
When the event is initialized, it is ready for dispatching.
In Internet Explorer before version 9, use the createEventObject method to create a new event object and the fireEvent method to dispatch it.
Syntax:
You can find the related objects in the Supported by objects section below.
Parameters:
Required. Reference to an event object to be dispatched. |
Return value:
Boolean that indicates whether the default action of the event was not canceled.
The default action can be canceled with the preventDefault method and the returnValue property.
When an event handler returns false, it also cancels the default action.
The getPreventDefault method can also be used to retrieve whether an event was canceled or not.
One of the following values:
The event was canceled. | |
The event was not canceled. |
Example HTML code 1:
This example illustrates the use of the dispatchEvent and fireEvent methods:
|
||||
<head> <script type="text/javascript"> function InitMouseDown (event) { if (event.initMouseEvent) { // all browsers except IE before version 9 var mousedownEvent = document.createEvent ("MouseEvent"); mousedownEvent.initMouseEvent ("mousedown", true, true, window, 0, event.screenX, event.screenY, event.clientX, event.clientY, event.ctrlKey, event.altKey, event.shiftKey, event.metaKey, 0, null); event.target.dispatchEvent (mousedownEvent); } else { if (document.createEventObject) { // IE before version 9 var mousedownEvent = document.createEventObject (window.event); mousedownEvent.button = 1; // left button is down event.srcElement.fireEvent ("onmousedown", mousedownEvent); } } } </script> </head> <body> <button onmouseover="InitMouseDown (event);" onmousedown="alert ('mousedown event occurred')"> Hover over this button! </button> </body> |
||||
|
||||
Did you find this example helpful?
|
Example HTML code 2:
This example illustrates the difference between the dispatchEvent and fireEvent methods. The dispatchEvent method invokes the default action of the event, the fireEvent method does not:
|
||||
<head> <script type="text/javascript"> function InitClick (event) { if (event.initMouseEvent) { // all browsers except IE before version 9 var clickEvent = document.createEvent ("MouseEvent"); clickEvent.initMouseEvent ("click", true, true, window, 0, event.screenX, event.screenY, event.clientX, event.clientY, event.ctrlKey, event.altKey, event.shiftKey, event.metaKey, 0, null); event.target.dispatchEvent (clickEvent); } else { if (document.createEventObject) { // IE before version 9 var clickEvent = document.createEventObject (window.event); clickEvent.button = 1; // left click event.srcElement.fireEvent ("onclick", clickEvent); } } } </script> </head> <body> <input type="checkbox" onmouseover="InitClick (event);" onclick="alert ('click event occurred')"/>Hover over the check box! </body> |
||||
|
||||
Did you find this example helpful?
|
Supported by objects:
CommentNode, document, DocumentFragment, TextNode, window, XMLDocument, XMLHttpRequest
HTML elements:
a, abbr, acronym, address, applet, area, b, base, basefont, bdo, bgsound, big, blink, blockquote, body, br, button, caption, center, cite, code, col, colgroup, comment, dd, del, dfn, dir, div, dl, dt, em, embed, fieldset, font, form, frame, frameset, h1, h2, h3, h4, h5, h6, head, hr, html, i, iframe, img, input:button, input:checkbox, input:file, input:hidden, input:image, input:password, input:radio, input:range, input:reset, input:search, input:submit, input:text, ins, isindex, kbd, keygen, label, legend, li, link, listing, map, marquee, menu, meta, nobr, noframes, noscript, object, ol, optgroup, option, p, param, plaintext, pre, q, rt, ruby, s, samp, script, select, small, span, strike, strong, style, sub, sup, table, tbody, td, textarea, tfoot, th, thead, title, tr, tt, u, ul, var, wbr, xml, xmp
Related pages:
External links:
User Contributed Comments