473,786 Members | 2,608 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Cross-browser alternative to click() event

Hi. I just ran into a situation where I want to emulate the IE
specific obj.click() syntax on an object on the webpage.

The most convenient thing for me is if I were able to just select my
object

document.getEle mentById('myobj ').click()

Do you know any cross-browser solutions to that? (must work in firefox
as a minimum).

Thanks

/Jesper Rønn-Jensen

Jan 30 '07 #1
7 10055
This may not be the best answer but see if you can get something along
these lines to work.I used it for drag and drop.

js script:
function mouseOver(ev){
ev = ev || window.event;
var target = ev.target || ev.srcElement;
if(!target) return;

if(target.id == 'DRAG'){
target.onmoused own = function(ev){
dragObject = this;
mouseOffset = getMouseOffset( this, ev);
return false;
}
}
}

function mouseDown(ev){
ev = ev || window.event;
var target = ev.target || ev.srcElement;

//code you want to execute.
}

document.onmous eover = mouseOver;
document.onmous edown = mouseDown;
//end script

html code:
<html>
<script src="js"></script>
<body>
<div id='drag'></div>
</body>
</html>

Let me know if that helps you!

Jan 30 '07 #2
On Jan 30, 4:57 pm, "Jesper Rønn-Jensen" <jespe...@gmail .comwrote:
Hi. I just ran into a situation where I want to emulate the IE
specific obj.click() syntax on an object on the webpage.

The most convenient thing for me is if I were able to just select my
object

document.getEle mentById('myobj ').click()

Do you know any cross-browser solutions to that? (must work in firefox
as a minimum).

Thanks

/Jesper Rønn-Jensen
click() is supported by FireFox.
http://developer.mozilla.org/en/docs/DOM:element.click
This example works well:

<div id="myobj" onclick="alert( 'Clicked!');">C lick me</div>
<script>
document.getEle mentById('myobj ').click();
</script>

Maybe you just need a cross-browser event binding method? Try Event
object from http://www.prototypejs.org.

Sincerely,
Alexander
http://www.alexatnet.com - PHP/Ajax notes, tips, tutorials
Jan 30 '07 #3
Adambrz, Alexander

Thanks for the tips. I'll look into it and see if that can help my
situation when I get back to work. I really appreciate your thoughts.
Click() in Firefox is new to me, so obviously there must be something
else bugging.

/Jesper

Feb 2 '07 #4
Alexander wrote:
click() is supported by FireFox. http://developer.mozilla.org/en/docs/DOM:element.click
Well, it turns out the page you're linking to explains that only
certain input elements will respond to click() in Gecko (firefox,
mozilla, etc.).

That explains why I can't get your example to work in firefox --
because the <divelement is not an input element.

According to the W3C DOM level 2 spec (that the link you provided
links to), this is how the browser should work:
click
Simulate a mouse-click. For INPUT elements whose type attribute
has one of the following values: "button", "checkbox", "radio",
"reset", or "submit".
(from http://www.w3.org/TR/DOM-Level-2-HTM...tml#ID-2651361)

Which means that IE is too generous in it's support according to the
spec.

Feb 2 '07 #5
VK
On Feb 3, 1:30 am, "Jesper Rønn-Jensen" <jespe...@gmail .comwrote:
Click() in Firefox is new to me, so obviously there must be something
else bugging.
click() is DOM 0 method (Netscape 2.0) so any normal browser - and any
browser pretending to be such - supports it in general. The problem is
the that applicability of this method was narrowed over the past years
as part of user interface protection. In the particular on different
browsers click() may be silently ignored for form upload control,
select control options and links. Overall currently you may relay on
click() only for form controls _except_ file upload control and select
control.
Custom events as suggested in other post do not change anything in the
security limitations: wherever click() is not allowed, custom events
will not work either.

The fact that you are happy with IE but asking about Firefox suggests
that your [object] is in fact hidden file upload control you are
triggering by click(). If I'm right then sorry but this bird won't
fly: on Gecko browsers it is a part of user interface protection.
Feb 2 '07 #6
VK
On Feb 3, 1:42 am, "Jesper Rønn-Jensen" <jespe...@gmail .comwrote:
Well, it turns out the page you're linking to explains that only
certain input elements will respond to click() in Gecko (firefox,
mozilla, etc.).

That explains why I can't get your example to work in firefox --
because the <divelement is not an input element.
Oh, so you need it for DIV? Sorry then to everyone, createEventObje c/
fireEvent (IE) + createEvent/dispatchEvent (Gecko) will work.
Be aware that artificial event are not propagated (do not "bubble
up"). It may be important or not for your case, but just to mention.

Feb 2 '07 #7
Mmm same problem here..
Here's my solution.
Note that this function would need more appropriate error handling
etc..
currently tested on firefox 2 and IE 6

---------------------8<----------------------------------

function doClick(obj) {
try {
var evt = document.create Event("MouseEve nts");
evt.initMouseEv ent("click", true, true, window,0, 0, 0, 0, 0,
false, false, false, false, 0, null);
var canceled = !obj.dispatchEv ent(evt);
if(canceled) {
// A handler called preventDefault
} else {
// None of the handlers called preventDefault
}
} catch(er) {
obj.click(); //IE
}
}

-------------------->8----------------------------------

just call this function like this:
doClick(documen t.getElementByI d(OBJECT_ID));

Check this out for more info:
http://developer.mozilla.org/en/docs....dispatchEvent
http://developer.mozilla.org/en/docs...initMouseEvent

Bye
Sebastian
www.sebastian.it

Feb 17 '07 #8

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

Similar topics

4
5584
by: David Peach | last post by:
Hello, hope somebody here can help me... I have a query that lists defects recorded in a user defined date range. That query is then used as the source for a Cross Tab query that cross-tabs count of defect type by calendar month. Defect types are stored in one table, defect transactions in another along with date etc. When I cross-tab the results, defect types that have no defects recorded against them appear as a blank (null) value. That...
2
1499
by: Sidorf | last post by:
Hi It's a little hard for me to explain my problem, but i'll try. I have an application in which i have a ManagerClass, many ClientClass-es ans some ControlClass. The ManagerClass creates one or more instances of ClientClass. ClientClass creates some controls from ControlClass. In the ControlClass i have derived some common control classes and added some functionalities.
4
3579
by: Bob | last post by:
Hi, Moving a project from .net 2003 -> 2005 Beta 2 Windows App. Main Window is start object. Main window spawns a thread. After doing some work this thread raises an interrupt. The event carries a reference to the class that raised it plus an instantiated, empty eventarg. RaiseEvent LineStateChange(Me, e) The 'RaiseEvent' errors in 2005 with Cross thread...
8
4855
by: Pieter | last post by:
Hi, I'm having some weird problem using the BackGroundWorker in an Outlook (2003) Add-In, with VB.NET 2005: I'm using the BackGroundWorker to get the info of some mailitems, and after each item I want to raise the ProgressChanged-event to update the DataGridView. It works fine when only one Progresschanged is fired, but at the second, third, fopurth etc it raises everytile a 'Cross-thread operation not valid"-exception on lmy...
11
8689
by: HairlipDog58 | last post by:
Hello, There are several 'cross-thread operation not valid' exception postings in the MSDN discussion groups, but none address my exact situation. I have authored a .NET component in Visual C# .NET 2003 that is capable of firing several types of events to notify user of errors, data changes, etc. The component uses a thread to perform background communications and if there is an error or data-change fires an event to notify the user....
3
5421
by: Pieter Coucke | last post by:
Hi, In my VB.NET 2005 application I'm generating and sending emails using the outlook-object model (2003). When a mail is Send (MailObject_Send), I raise an event in a global class, that is caught by all my forms that than refresh the lists with emails. But on the moment I do a MyDataGrid.DataSource = nothing, I get this "Cross-thread operation not valid"-exception:
2
1297
by: VK | last post by:
Cross-Domain Interactor (CDI) StarGates 0.9 is released I believe that the post "JSON - Cross domain request" <http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/06ba7973660bf018> was the 100th in this newsgroup asking about cross-domain interaction using ajaxoids. In commemoration of this historic event :-) I've downgraded one of my Perl spiders and adjusted it for pieceful purposes :-): namely to use it in...
10
6889
by: Daniel | last post by:
Hi guys I have a form with my gui on, just some list boxes. And a class that handles incoming data. i want to, on receiving data, update my gui. However even though i have an instance created in my class handling the receiving data of the gui form if i try and update the gui directly i get a
4
5319
by: Paul Cheetham | last post by:
Hi, I have a couple of classes that I am using to read a swipe-card reader attached to the serial port (c# VS 2005 Pro). I have a SerialComm class which actaully reads the serial port, and a CardReader class which validates the number etc. Neither of these classes inherit from any other classes / controls. The SerialComm class is instantiated by the Cardreader class, so in the
6
2377
by: AliR \(VC++ MVP\) | last post by:
Hi everyone, I have a Socket derived class that parses the received data into a message, populates a object with that information and then tries to send an event to inform the owner of the socket that there is message to be processed. But this is not working as the socket receiver method seems to be in a different thread then the form that owns the socket. The receive callback gets the data from the socket and process the...
0
9650
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9497
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
10363
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
10164
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...
1
10110
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7515
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
5398
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
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.