Closed
Bug 281699
Opened 21 years ago
Closed 21 years ago
dispatchEvent of onclick event to a link does not invoke the href
Categories
(Core :: DOM: Events, defect)
Tracking
()
RESOLVED
WONTFIX
People
(Reporter: gibson.becky, Unassigned)
Details
Attachments
(1 file)
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8b) Gecko/20050130
Build Identifier: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8b) Gecko/20050130
In Mozilla 1.7.2 if I dispatched an onclick event to an <a> element, the href
for the element would be invoked. In this build it is not. I have to
specifically have an onclick handler.
Given the following code and JavaScript:
<a href="javascript:alert('goto next link');" id="nextLink" >Next > </a>
<script>
var theLink=document.getElementById("nextLink");
var mClick = document.createEvent("MouseEvents");
mClick.initEvent("click",true, true);
theLink.dispatchEvent(mClick);
</script>
Reproducible: Always
Steps to Reproduce:
1.create a link on the page
2.catch some key sequence - my example uses alt and left/right arrow
3.when key squence is received - dispatch a click event to the link element
Actual Results:
the link is not invoked.
Expected Results:
onclick dispatched to an <a> element should invoke the href. I don't want to add
an onclick handler because the default behavior of clicking on the link is changed.
I have an example file but don't see where I can attach. Here is the code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Dispatch Event Example</title>
<script language="javascript">
//<![CDATA[
var isIE = false;
if (navigator.userAgent.indexOf("MSIE") != -1) {
isIE = true;
}
function keySequence(e) {
var evt = (e != null) ? e : ((event) ? event : null); // Mozilla will include
e parameter
var key= evt.keyCode;
var bCtrl = evt.ctrlKey;
var bAlt = evt.altKey;
var bDispatch = (key == 37 || key == 38 || key == 39 || key == 40);
if ( (bCtrl == true || bAlt == true ) && bDispatch == true) {
var theLink = null;
// invoke the onclick of the associated next(39) or prev(37) button on the page
if (key == 37 && bCtrl) { // do previous
theLink = document.getElementById("prev");
}
else if (key == 39 && bCtrl) { // do next
theLink = document.getElementById("next");
}
else if(key == 38 && bAlt ) {
theLink = document.getElementById("prevLink");
}
else if (key == 40 && bAlt ) {
theLink = document.getElementById("nextLink");
}
if (theLink != null) {
if (theLink.click) {
theLink.click();
//var ieClick = document.createEventObject();
//theLink.fireEvent("onclick",ieClick);
}
else {
var mClick = document.createEvent("MouseEvents");
mClick.initEvent("click",true, true);
theLink.dispatchEvent(mClick);
alert("did Moz dispatch"); // so I know code was reached
}
}
}
return true;
}
//]]>
</script>
</head>
<body onkeyup="keySequence(event);">
<H2>Dispatch Event Example</h2>
<p>This example shows how to dispatch an event in IE or Mozilla. Press
ctrl-left arrow to invoke the previous button.
Press ctrl-right arrow to invoke the next button. For demonstration purposes the
buttons just invokve a message box with the
intended action rather than navigating away from this page. Press alt-up to
invoke the previous link and alt-down
to invoke the next link.</p>
<p>Note that invoking the links programmatically via alt-up/ alt-down using
fireEvent() does not work in IE.
Mozilla will execute the href when an anchor tag receives an onclick event. I
could not find away to
get IE to execute the anchor tag href via an onclick, onkeypress, onmouseup or
onmousedown via fireEvent - but, I can use linkElement.click() in IE. . </p>
<div><span style="float:left;"><button onclick="javascript:alert('goto
previous');" id="prev">< Previous</button><br />
<<a href="javascript:alert('goto previous link');" id="prevLink">
Previous</a></span>
<span style="float:right;"><button onclick="javascript:alert('goto next');"
id="next" >Next > </button>
<br /><a href="javascript:alert('goto next link');" id="nextLink" >Next >
</a></span>
</div>
<br/><br /><br/>
</body>
</html>
| Reporter | ||
Comment 1•21 years ago
|
||
Updated•21 years ago
|
Summary: dispatchEvent of onclick event to a link does not invoke the href → dispatchEvent of onclick event to a link does not invoke the href
Comment 2•21 years ago
|
||
Untrusted events are no longer allowed to trigger links, for security reasons.
See bug 265176.
If you need this functionality, I believe you can request the
UniversalBrowserWrite capability... then your events will be trusted.
Status: UNCONFIRMED → RESOLVED
Closed: 21 years ago
Resolution: --- → WONTFIX
You need to log in
before you can comment on or make changes to this bug.
Description
•