Results 1 to 2 of 2

Thread: Dynamic event handlers

  1. #1
    Join Date
    Jun 2006
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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. #2
    Join Date
    Jun 2006
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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>

Bookmarks

Posting Permissions

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