473,385 Members | 1,185 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How to trigger event by script

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.
--

Ralph
Nov 23 '06 #1
13 36052
Ralph <on**@remove.op.plwrote in news:xC79h.13660$9v5.10396
@newssvr29.news.prodigy.net:
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.
No, it's a function of the DOM object (in this case, your image).

http://developer.mozilla.org/en/docs/DOM:element.click
Nov 23 '06 #2
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>
--
Jonas Raoni Soares Silva
http://www.jsfromhell.com
Nov 23 '06 #3
Ralph wrote:
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....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>
--
Rob

Nov 23 '06 #4
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

--
Stephane Moriaux et son (moins) vieux Mac déjà dépassé
Stephane Moriaux and his (less) old Mac already out of date
Nov 23 '06 #5
VK

ASM wrote:
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.

Nov 23 '06 #6
ASM
VK a écrit :
ASM wrote:
>Sorry, doesn't work with my Safari :

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)

--
Stephane Moriaux et son (moins) vieux Mac déjà dépassé
Stephane Moriaux and his (less) old Mac already out of date
Nov 23 '06 #7

ASM wrote:
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
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.

--
Rob

Nov 23 '06 #8
VK wrote:
ASM wrote:
Sorry, doesn't work with my Safari :

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?
--
Rob

Nov 23 '06 #9
ASM
RobG a écrit :
ASM wrote:
>RobG a écrit :
>> 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
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

--
Stephane Moriaux et son (moins) vieux Mac déjà dépassé
Stephane Moriaux and his (less) old Mac already out of date
Nov 23 '06 #10
RobG said the following on 11/23/2006 9:24 AM:
VK wrote:
>ASM wrote:
>>Sorry, doesn't work with my Safari :
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.
You actually expected more of VK? <g>

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Nov 23 '06 #11
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.

Nov 23 '06 #12
VK
:-) 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?
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 ;-)
But the script generated events have numerous security limitations in
comparison with hardware (input devices) produced events.

Such as?
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.

Nov 23 '06 #13

VK wrote:
:-) 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?

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.
>
But the script generated events have numerous security limitations in
comparison with hardware (input devices) produced events.
Such as?

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?
--
Rob

Nov 24 '06 #14

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

Similar topics

2
by: Mozzie ³ | last post by:
Can 'onbeforeunload' (ie) be used to trigger a javascript? I can't find any info on how or if this is possible. Regards.
2
by: kj | last post by:
How does one trigger an event programmatically? 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...
2
by: Matt | last post by:
The following code won't work, because onchange event is not trigger when drop down size = 1. But if I make the size="2" or greater, then it will work. Is that true? please advise. thanks!! ...
8
by: Frank van Vugt | last post by:
Hi, If during a transaction a number of deferred triggers are fired, what will be their execution order upon the commit? Will they be executed in order of firing or alfabetically or...
0
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...
1
by: daonho | last post by:
I tried to use javascript to trigger up the button click function when user press enter key from the textbox. This function work fine with a single button click such has login page. However, if the...
11
by: tracy | last post by:
Hi, I really need help. I run this script and error message appeal as below: drop trigger log_errors_trig; drop trigger log_errors_trig ERROR at line 1: ORA04080: trigger 'LOG_ERRORS-TRIG'...
1
Ciary
by: Ciary | last post by:
hi all, i wasnt really sure where to post this since it's a combination of Javascript and PHP. i'm trying to make a fileuploader without reloading the page. my structure: at the moment, i can...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.