...

View Full Version : Dynamic event handlers



igraws
06-01-2006, 08:16 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, 09:38 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>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum