I thought at first that this might be a quasi religious question (see recent posts for more explanation on that).
The DOM events of an element are a part of an element's properties.
Except when they are assigned via attachEvent or addEventListener.
I think that this is a huge hole in DOM level 2 that will be corrected at some time in the future.
So say you have:
Code:
<a id="example" onclick="alert(this.href);return false;" href="some.htm">whatever</a>
This will work (we are focusing our attention on the 'Check' button):
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function getEvent(id){
alert(document.getElementById(id).onclick);
}
</script>
</head>
<body>
<a id="example" onclick="alert(this.href);return false;" href="some.htm">whatever</a><br>
<input type="button" value="Check" onclick="getEvent('example');">
</body>
</html>
Or this:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function getEvent(id){
alert(document.getElementById(id).onclick);
}
window.onload = function(){
document.getElementById('example').onclick = function(){
alert(this.href);return false;
}
}
</script>
</head>
<body>
<a id="example" href="some.htm">whatever</a><br>
<input type="button" value="Check" onclick="getEvent('example');">
</body>
</html>
But not this:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function getEvent(id){
alert(document.getElementById(id).onclick);
}
function giveHref(e){
e = e? e : window.event;
var t = e.target? e.target : e.srcElement;
alert(t.href);
if (e.preventDefault)
e.preventDefault();
return false;
}
window.onload = function(){
if(typeof window.addEventListener!='undefined')
document.getElementById('example').addEventListener('click', giveHref, false);
else if(typeof window.attachEvent!='undefined')
document.getElementById('example').attachEvent('onclick', giveHref);
};
</script>
</head>
<body>
<a id="example" href="some.htm">whatever</a><br>
<input type="button" value="Check" onclick="getEvent('example');">
</body>
</html>
But the onclick event of the a tag will be identical in all three examples.
Bookmarks