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 2 of 2
  1. #1
    New Coder
    Join Date
    Aug 2003
    Location
    Israel
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Dynamic event handlers

    Hi all. Here is a little sample code:

    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)">

  • #2
    New Coder
    Join Date
    Aug 2003
    Location
    Israel
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I found the solution:

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


  •  

    Posting Permissions

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