How to trigger event by script

R

Ralph

Hi

Is it possible to trigger the certain event from JS function?

I have an image with on click event handler assigned. Now if like to
trigger this event for this image from some other function.
 
R

RobG

Ralph said:
Hi

Is it possible to trigger the certain event from JS function?

I have an image with on click event handler assigned. Now if like to
trigger this event for this image from some other function.

You can use dispatchEvent and fallback to calling the element's click
method:

<URL: http://developer.mozilla.org/en/docs/DOM:element.dispatchEvent >


A quick example using the above reference (tested in Fx 2.0, IE 6 and
Opera 9):

<script type="text/javascript">

function simulateClick(elId) {
var evt;
var el = document.getElementById(elId);
if (document.createEvent){
evt = document.createEvent("MouseEvents");
evt.initMouseEvent("click", true, true, window,
0, 0, 0, 0, 0, false, false, false, false, 0, null);
}
(evt)? el.dispatchEvent(evt):(el.click && el.click());
}

</script>
<div onclick="alert('Div got click too');">
<input type="checkbox" id="cb_01">A checkbox
</div>
<br>
<button onclick="simulateClick('cb_01');">Simulate click</button>
 
J

Jonas Raoni

Ralph escreveu:
Is it possible to trigger the certain event from JS function?
Yes.

I have an image with on click event handler assigned. Now if like to
trigger this event for this image from some other function.

You can call "image.onclick()", but the right way to invoke events is by
creating an event. Take a look on these links (the standard and the IE
ways of doing it):

<URL:http://developer.mozilla.org/en/docs/DOM:document.createEvent>
<URL:http://msdn.microsoft.com/workshop/author/dhtml/reference/methods/createeventobject.asp>
 
A

ASM

RobG a écrit :
A quick example using the above reference (tested in Fx 2.0, IE 6 and
Opera 9):

<script type="text/javascript">

function simulateClick(elId) {

Sorry, doesn't work with my Safari :
Value undefined (result of expression evt.initMouseEvent) is not object.

iCab thinks button is a form reset button
and does nothing
 
V

VK

ASM said:
Sorry, doesn't work with my Safari :

With the same success you could say "It doesn't work on NCSA Mosaic"
:) Did you try on some descent browser people are using? Camino
(http://www.caminobrowser.org) would be the best choice.

But the script generated events have numerous security limitations in
comparison with hardware (input devices) produced events. So depending
on what OP wants they may do the trick or may not.
 
A

ASM

VK a écrit :
With the same success you could say "It doesn't work on NCSA Mosaic"

Ho! Forgotten to try with my NC4.5 :-/
:) Did you try on some descent browser people are using?

Yes (not descent IE5.2 and descent FF2 (Mac)) and it works.

I hoped to get a light about Safari ?!
But the script generated events have numerous security limitations in
comparison with hardware (input devices) produced events. So depending
on what OP wants they may do the trick or may not.

Certainly,
and now he knows that doesn't work with "my" Safari 1.3.2 :)
(don't know about Safari 2)
 
R

RobG

ASM said:
RobG a écrit :

Sorry, doesn't work with my Safari :
Value undefined (result of expression evt.initMouseEvent) is not object.

iCab thinks button is a form reset button
and does nothing

Sorry, I didn't have Safari available at the time. I knew if I assumed
support for all event methods based on createEvent I'd get called to
account! Safari seems to support the click() method on DOM elements,
so I suppose they didn't bother to add initMouseEvent(). Safari is
kinda halfway between Fx and IE on many things.

The following works in Safari 2 and shouldn't throw errors:

function simulateClick(elId) {
var evt;
var el = document.getElementById(elId);
if (document.createEvent){
evt = document.createEvent("MouseEvents");
if (evt.initMouseEvent){
evt.initMouseEvent("click", true, true, window,
0, 0, 0, 0, 0, false, false, false, false, 0, null);
} else {
evt = false;
}
}
(evt)? el.dispatchEvent(evt):(el.click && el.click());
}

It may still have issues though.
 
R

RobG

VK said:
With the same success you could say "It doesn't work on NCSA Mosaic"

Is that it? Is that all you have offer? Did you discover that Safari
actually supports element.click() just like IE? Perhaps you should
pour some scorn on IE too - its support for W3C standards is well below
that of Safari's, and it is derived directly from Mosaic.

:) Did you try on some descent browser people are using? Camino
(http://www.caminobrowser.org) would be the best choice.

That infers that you have done some evaluation that establishes Camino
is "better" based on some objective criteria. Care to post it?

But the script generated events have numerous security limitations in
comparison with hardware (input devices) produced events.

Such as?
 
A

ASM

RobG a écrit :
The following works in Safari 2 and shouldn't throw errors:

function simulateClick(elId) {
var evt;
var el = document.getElementById(elId);
if (document.createEvent){
evt = document.createEvent("MouseEvents");
if (evt.initMouseEvent){
evt.initMouseEvent("click", true, true, window,
0, 0, 0, 0, 0, false, false, false, false, 0, null);
} else {
evt = false;
}
}
(evt)? el.dispatchEvent(evt):(el.click && el.click());
}

Yheaaa! Greatttt ! : OK IE 5.2, FF 2, Safari 1.3.2, Opera 9.0

iCab : i'll go to see if there is a new version
 
R

Randy Webb

RobG said the following on 11/23/2006 9:24 AM:
Is that it? Is that all you have offer? Did you discover that Safari
actually supports element.click() just like IE? Perhaps you should
pour some scorn on IE too - its support for W3C standards is well below
that of Safari's, and it is derived directly from Mosaic.

You actually expected more of VK? <g>
 
V

VK

RobG wrote:
<quote>
A quick example using the above reference (tested in Fx 2.0, IE 6 and
Opera 9):
function simulateClick(elId) {
var evt;
var el = document.getElementById(elId);
if (document.createEvent){
evt = document.createEvent("MouseEvents");
evt.initMouseEvent("click", true, true, window,
0, 0, 0, 0, 0, false, false, false, false, 0, null);
}
(evt)? el.dispatchEvent(evt):(el.click && el.click());
}
</quote>

ASM wrote:
<quote>
Sorry, doesn't work with my Safari :
Value undefined (result of expression evt.initMouseEvent) is not
object.
</quote>

VK wrote:
<quote>
ith the same success you could say "It doesn't work on NCSA Mosaic"
:) Did you try on some descent browser people are using?
</quote>

RobG wrote:
<quote>
Is that it? Is that all you have offer? Did you discover that Safari
actually supports element.click() just like IE?
</quote>

As you may see (when the conversation chain is restored) Safary doesn't
support click() method "just like IE": otherwise it wouldn't fall on
wrong if-else branch. What Safari does (from the time it first
appeared) it supports a narrow set of features in a generously twisted
way but at the same time it covers all possible "holes" with loophole
(blackhole) methods.

Is it possible to make a workable solution for Safari? Almost always
yes, sure: just like it would be possible for Netscape 4.x. And just
like with Netscape 4.x currently the expenses would overcome benefits -
by my calculations which may differ a lot from someone else's
calculations.

I do not see myself obligated of being "supportive and respectful" to
any single browser on the market as long as it's not IE ;-) I call junk
on junk: and Safari prior 2.x is a jankiest junk in the junk book. And
that is an ugly stain in a company like Apple who always put quality of
the final product as their top priority.

At the same time, as anyone may notice, if I know how to fix this poor
mutant for this or that particular situation - I always say how.
 
V

VK

:) Did you try on some descent browser people are using? Camino
That infers that you have done some evaluation that establishes Camino
is "better" based on some objective criteria. Care to post it?

Standards support, behavior as documented, necessary advanced features
(XSLT, SVG,...). Really, what list should it be?

Safari is an ugly junk. Proof... Well, install Safari 1.3.x and try 100
more-or-less sophisticated cross-browser scripts (starting say from
JavaScript ToolBox). If at least half (50) of them will work as
expected, we may talk further ;-)

RobG, are you so upset of me calling Safari bad names? Please note that
I'm talking about a particular software product, not Apple company as
such. So why these rithoric questions?

createEvent / createEventObject generated events have a set of
limitations preventing programmer from stealing user interface.
Relevant questions were posed at c.l.j. and answered a number of times.
 
R

RobG

VK said:
Standards support, behavior as documented, necessary advanced features
(XSLT, SVG,...). Really, what list should it be?

Safari is an ugly junk. Proof... Well, install Safari 1.3.x and try 100
more-or-less sophisticated cross-browser scripts (starting say from
JavaScript ToolBox). If at least half (50) of them will work as
expected, we may talk further ;-)

I used Safari 1.3 for a year or so for all sorts of purposes, from
on-line banking with 5 different banks, 3 different share trading
systems, eBay (of course) and sundry other e-commerce sites - event
bookings, flights, on-line purchases, etc. and had very few problems.

I could have used Firefox (I use it exclusively on Windows) but Safari
suits me better on Mac. Recently I've been playing with Opera - v 9 is
great, it may have won me over.
RobG, are you so upset of me calling Safari bad names? Please note that
I'm talking about a particular software product, not Apple company as
such.

Nor am I. The more stick Apple cops over Safari the more I like it,
they may respond and make it better. I just get annoyed at gratuitous
mud slinging.
So why these rithoric questions?

Because I really do want to know how initiating events via
dispatchEvent is any more of a security hazard that initiating them any
other way. I can't see that it is, convince me otherwise.
createEvent / createEventObject generated events have a set of
limitations preventing programmer from stealing user interface.
Relevant questions were posed at c.l.j. and answered a number of times.

A search on "createEvent security" returns two threads other than this
one. The only person to use the word "security" is you, and you don't
provide any examples of how createEvent is a security issue, you just
make assertions that particular behaviour must be because of some
security feature, e.g.:

"But trying to dispatch artificial event to an element in
violation of its EventProducer model *seems* to lead to the security
exeption and script abort in FireFox."


So the challenge stands - what are the security implications of
createEvent over and above other types of script-initiated events?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top