...

View Full Version : Passing the event object to an onclick event handler in IE



gooseus
09-04-2008, 12:09 AM
I've been web coding for years now and have spent a lot of time searching through forums, tutorials and references for answers to my questions, but I figured I'd start posting online to help contribute to the World Wide Knowledge Base.

anyways, I've taken over a large site which is going in to Beta soon and I made a little javascript function which will pop-up a feedback form when one of the testers clicks on a page with the Shift and Alt keys pressed down.



document.onclick = function(event) { Feedback(event); }

function Feedback(e) {
var shiftDn = e.shiftKey;
var altDn = e.altKey;
var x = e.screenX;
var y = e.screenY;

if(shiftDn && altDn){
window.open("/BetaFeedback.asp","_blank","height=500,width=500,status=no,location=no,menubar=no,scrollbars=no,toolbar=no,titlebar=no,left="+x+",top="+y);
}
}


I wanted to add this code to every page on the site without having to physically add an onclick event to the body tag (I was going to include it in a .js file which is already included on every page) which I'd then have to remove from every page when the beta was done (of which there are hundreds since my predecessor didn't seem to understand the idea of dynamic content despite giving everything a .asp extension).

This code works perfectly in Mozilla, but doesn't work in IE... it seems to have trouble passing the event handler through to the function and gives the error:

" 'shiftKey' is null or not an object "

Anyone know a cross-browser solution to this? Thanks in advance.

Arty Effem
09-04-2008, 01:25 AM
This code works perfectly in Mozilla, but doesn't work in IE... it seems to have trouble passing the event handler through to the function and gives the error:

" 'shiftKey' is null or not an object "



In IE the most recent event object is stored in window.event.
<script type="text/javascript">
document.onclick = function(event) { Feedback(event); }

function Feedback(evt)
{
var e=evt||window.event;

var shiftDn = e.shiftKey;
var altDn = e.altKey;
var x = e.screenX;
var y = e.screenY;

if(shiftDn && altDn){
window.open("/BetaFeedback.asp","_blank","height=500,width=500,status=no,location=no,menubar=no,scrollbars=no,toolbar=no,titlebar=no,left="+x+",top="+y);
alert('shift and alt down on click');
}

}
</script>

gooseus
09-04-2008, 04:38 AM
Awesome, that worked, thanks a lot.

jkd
09-04-2008, 06:00 AM
If you use attachEvent(), the event object is passed as an argument to the handler function.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum