Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Sep 2008
    Location
    Southbay, CA
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Passing the event object to an onclick event handler in IE

    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.

    Code:
    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.

  • #2
    Banned
    Join Date
    May 2006
    Location
    England
    Posts
    664
    Thanks
    0
    Thanked 84 Times in 84 Posts
    Quote Originally Posted by gooseus View Post
    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.
    Code:
    <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>

  • #3
    New to the CF scene
    Join Date
    Sep 2008
    Location
    Southbay, CA
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Awesome, that worked, thanks a lot.

  • #4
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    If you use attachEvent(), the event object is passed as an argument to the handler function.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •