Default action not taken when using javascript to fire a click event in IE

J

jmpinchot

I'm trying to automate Internet Explorer using javascript. I want to
be able to tell IE whether or not to right/alt/ctrl/shift click an
element on the page, so I'm creating an event object by calling
document.createEventObject() and setting the appropriate values on the
event object and then calling element.fireEvent.

The problem is that if I call that method on either an anchor element
or submit input element, either the form is not submitted or IE
doesn't browse to the desired url.

Calling element.click() fixes the problem with allowing the default
action to take place, but doesn't let me control the properties of the
event object.

Is there a way to modify how click() creates the event object, or is
there a way to force the default action for the target element to take
place after calling fireEvent() on it?

Heres the code related to creating and firing the event:

var ieEvent = element.ownerDocument.createEventObject();
ieEvent.detail = 0;
ieEvent.screenX = screenX;
ieEvent.screenY = screenY;
ieEvent.clientX = clientX;
ieEvent.clientY = clientY;
ieEvent.ctrlKey = (clickOptions) ? clickOptions.ctrl : false;
ieEvent.altKey = (clickOptions) ? clickOptions.alt : false;
ieEvent.shiftKey = (clickOptions) ? clickOptions.shift :
false;
ieEvent.metaKey = false;
if (eventType == "mousedown" || eventType == "mouseup") {
ieEvent.button = 1;
} else {
ieEvent.button = 0;
}
ieEvent.relatedTarget = null;
element.fireEvent('on' + eventType, ieEvent);
 
C

Csaba Gabor

I'm trying to automate Internet Explorer usingjavascript. I want to
be able to tellIEwhether or not to right/alt/ctrl/shift click an
element on the page, so I'mcreatingan event object by calling
document.createEventObject() and setting the appropriate values on the
event object and then calling element.fireEvent.

The problem is that if I call that method on either an anchor element
or submit input element, either the form is not submitted orIE
doesn't browse to the desired url.

Calling element.click() fixes the problem with allowing the default
action to take place, but doesn't let me control the properties of the
event object.

Is there a way to modify how click() creates the event object, or is
there a way to force the default action for the target element to take
place after calling fireEvent() on it?

Heres the code related tocreatingand firing the event:

var ieEvent = element.ownerDocument.createEventObject();
ieEvent.detail = 0;
ieEvent.screenX = screenX;
ieEvent.screenY = screenY;
ieEvent.clientX = clientX;
ieEvent.clientY = clientY;
ieEvent.ctrlKey = (clickOptions) ? clickOptions.ctrl : false;
ieEvent.altKey = (clickOptions) ? clickOptions.alt : false;
ieEvent.shiftKey = (clickOptions) ? clickOptions.shift :
false;
ieEvent.metaKey = false;
if (eventType == "mousedown" || eventType == "mouseup") {
ieEvent.button = 1;
} else {
ieEvent.button = 0;
}
ieEvent.relatedTarget = null;
element.fireEvent('on' + eventType, ieEvent);


If I were to be developing this, I would try sending the appropriate
modifier down key (keydown) event. This has made a difference in
Firefox where I am sending keyboard events. Simply setting the
appropriate modifiers is not always sufficient. Also, if you are
firing multiple events make sure that you can fire them immediately
after each other (vs. having a delay with an appropriate
window.setTimeout).

Csaba Gabor from Vienna
 
D

David Mark

I'm trying to automate Internet Explorer using javascript. I want to
be able to tell IE whether or not to right/alt/ctrl/shift click an
element on the page, so I'm creating an event object by calling
document.createEventObject() and setting the appropriate values on the
event object and then calling element.fireEvent.

The problem is that if I call that method on either an anchor element
or submit input element, either the form is not submitted or IE
doesn't browse to the desired url.

It doesn't work that way. The fireEvent method will call event
handlers attached to the element.
Calling element.click() fixes the problem with allowing the default
action to take place, but doesn't let me control the properties of the
event object.

It isn't a problem, so you can't fix it. What you describe is the
designed behavior.

FWIW, If you want to navigate to the destination of a link, read its
href property and set the location object appropriately. If you want
to submit a form, use the submit method of the form. Why are you
trying to do everything indirectly? What exactly would the control,
shift and alt keys affect when submitting a form or clicking a link?
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top