473,785 Members | 2,553 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to trigger event programmaticall y?

kj


How does one trigger an event programmaticall y? I'm interested in
how to do this in both the "Level 0" event model as well as in the
DOM Level 2 event model.

Thanks!

kj
--
NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded.
Jul 23 '05 #1
2 11366
On Mon, 9 Aug 2004 15:41:57 +0000 (UTC), kj <so***@987jk.co m.invalid>
wrote:
How does one trigger an event programmaticall y? I'm interested in
how to do this in both the "Level 0" event model as well as in the
DOM Level 2 event model.


With a conforming browser, dispatching an event is trivial: it's built
into the event model. It will even result in performing the default action
associated with that event and the target element. For example:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html lang="en" dir="ltr">
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Script-Type" content="text/javascript">

<title>Dispatch ing events</title>

<script type="text/javascript">
/* Add the event listener */
function init() {
var elm = null;

if(document.get ElementById) {
elm = document.getEle mentById('test' );
}
if(elm && elm.addEventLis tener) {
elm.addEventLis tener('click', function() {
alert('Level 2 listener activated');
}, false);
}
}

/* Dispatch a click event into the document tree
*
* Note: I would have called this function fireEvent, or
* dispatchEvent, however, this would have resulted in the
* browser-supplied functions (former in IE, latter in DOM-
* compliant browsers) being called. Be sure to avoid that.
*/
function doEventDispatch () {
var evt = null,
elm = null;

if(document.get ElementById) {
elm = document.getEle mentById('test' );
}
if(document.cre ateEvent) {
evt = document.create Event('MouseEve nts');
}
if(elm && elm.dispatchEve nt && evt && evt.initMouseEv ent) {
evt.initMouseEv ent(
'click',
true, // Click events bubble
true, // and they can be cancelled
document.defaul tView, // Use the default view
1, // Just a single click
0, // Don't bother with co-ordinates
0,
0,
0,
false, // Don't apply any key modifiers
false,
false,
false,
0, // 0 - left, 1 - middle, 2 - right
null); // Click events don't have any targets other than
// the recipient of the click
elm.dispatchEve nt(evt);
}
}
</script>
</head>

<body onload="init()" >
<div>
<a id="test" href="http://www.google.com/"
onclick="alert( 'Level 0 listener activated');">T est</a>
</div>
<div style="margin-top: 1em">
<button type="button"
onclick="doEven tDispatch()">Di spatch event</button>
</div>
</body>
</html>

As you can see, clicking the link or the button results in exactly the
same response from the browser, though the click event generated from the
link will have co-ordinates set, rather than zeros (0). Be aware that
every parameter must be specified in the init<type>Event methods,
otherwise the event won't be initialised properly and will result in an
exception.

Of course IE will have none of this. Whilst you can dispatch artificial
events into the document tree (using Element.fireEve nt), only listeners
will be dispatched. Actions associated with that event will not be
performed.

As for listeners set through HTML attributes or object properties, this
too is limited. The most you can do here is call the listeners on each
element. However, to do this, you'll probably need to have DOM support in
order to get a reference to the target element (unless you can use one of
the standard collections). If you want to allow the event to bubble up
into parent elements, you'll also need to have DOM support (so you can use
Node.parentNode ). And, just like with IE, you cannot get the browser to
perform any actions associated with an element.

So, as you can see, only the DOM approach offers real functionality.
Unfortunately, it is only subset of browsers. As such, make sure that you
thoroughly feature test (like I've done above) before deploying code on
the Web.

Hope that helps,
Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail
Jul 23 '05 #2
kj wrote:
How does one trigger an event programmaticall y? I'm interested in
how to do this in both the "Level 0" event model as well as in the
DOM Level 2 event model.

Thanks!

kj


To trigger an event you basically just call the event handler for that
element:

<a href="#" onclick="alert( 'Link One onclick!');retu rn false;">Link
One</a>
<a href="#" onclick="docume nt.links[0].onclick();retu rn false;">Link
Two</a>

Alternatively you can use dispatchEvent() <url:
http://www.mozilla.org/docs/dom/domr...6.html#1028419 /> and
fireEvent() <url:
http://msdn.microsoft.com/workshop/a.../fireevent.asp
/>.

--
Grant Wagner <gw*****@agrico reunited.com>
comp.lang.javas cript FAQ - http://jibbering.com/faq

Jul 23 '05 #3

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

Similar topics

6
3448
by: Dave C. | last post by:
Hello, I have created the following trigger: CREATE TRIGGER tr_UDFTest ON UserDefinedFields FOR INSERT, UPDATE AS DECLARE @foobar varchar(100) SELECT @foobar= foobar FROM inserted IF ( @foobar = 'foobar') INSERT INTO LogTable (LogText) values ('Found foobar')
17
61445
by: Mike Gratee | last post by:
Is it possible to use JavaScript to cause the browser to click a link on a page and have the browser act exactly like the user had clicked on the link directly? In other words, I need to programmatically issue a JavaScript statement which causes the browser to act just like the user clicked on a link in my page. And if that link has an onClick JS event defined, I'd want that onClick event to execute too, exactly the same as if the user...
5
6829
by: Carlo Marchesoni | last post by:
From an aspx page (A.aspx) I open another one (B.aspx - for table lookup). When the user selects an entry in B.aspx I would like to force a button's event in A.aspx to be fired. I guess the only way is using javascript - does anybody have a sample for this ? Thanks
2
4189
by: Rolf Falnes | last post by:
Hi. Does anyone know how to trigger the doubleclick mouse event in a datagrid cell (DatagridTextbox )? Regards, Rolf Falnes Datapartner AS
1
3520
by: Tony | last post by:
Hi, I have two forms A and B, both opened. In form A, I programmatically change the Date of Birth field of the current record of form B. I noticed that form B automatically displays the new data correctly. However, when I tried trapping the After Update, On Dirty, On Change, and On Current events of the Date of Birth text box, none of them gets triggered. I want to trap this change event so that in form B, I can programmatically...
0
900
by: rodchar | last post by:
hey all, can someone please show me the easiest way to add a trigger to an updatePanel programmatically? all the way down to which event handler it should go in. thanks, rodchar
0
1906
by: OceanBreeze | last post by:
I have added a LinkButton to a table cell programmatically inside the Page_Load method. I also added a custom event to that link button. The same custom event is valid for all the link buttons. The table gets successfully displayed on the browser with the LinkButton. When I click on the link button in the browser, my control does not go inside the custom even that I wrote for the link button. I have a table control as ProviderTbl in which...
6
6843
by: wugon.net | last post by:
Hi , Anyone know how to monitor db2 trigger activity ? We suffer some trigger issue , and we try to monitor trigger's behavior use event monitor and db2audit, but both tools can not get trigger's sql statment and cost, have other tools can get trigger's executing sql statment and cost ? our test case as follow: Env:
0
2638
by: wugon.net | last post by:
Hi , Anyone know how to monitor db2 trigger activity ? We suffer some trigger issue today and we try to monitor trigger's behavior use event monitor and db2audit, but both tools can not get trigger's sql statment and cost, have other tools can get trigger's executing sql statment and cost ? our test case as follow: Env:
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10324
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
10147
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...
0
8971
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...
1
7499
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6739
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5380
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...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3645
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.