PDA

View Full Version : Dynamic event handlers



igraws
06-01-2006, 07:48 AM
Hi all. Here is a little sample code:



<html>
<head>
<script type="text/javascript">
function BodyClick() {
// How to access the event object here?
alert(window.event.shiftKey);
}
function WindowLoad() {
document.body.onclick = BodyClick;
}
window.onload = WindowLoad;
</script>
</head>
<body></body>
</html>


It does not work in Firefox. How to make it work?

My only requirement is that I need to assign the BodyClick() handler
dynamically in script (not statically in HTML). So I cannot use this solution:
<body onclick="BodyClick(event)">

igraws
06-01-2006, 08:32 AM
I found the solution:



<html>
<head>
<script type="text/javascript">
function BodyClick(E) {
if (!E) E = window.event;
alert(E.shiftKey);
}
function WindowLoad() {
document.body.onclick = BodyClick;
}
window.onload = WindowLoad;
</script>
</head>
<body>
Click here
</body>
</html>