Dynamically adding event handlers to events using javascript

Discussion in 'JavaScript' started by vikben, Nov 14, 2007.

  1. #1
    <html>
    <head>
    <script type="text/javascript">
    function changeSize(strTemp)
    {alert(strTemp);}
    </script>
    </head>
    <body>
    <script type='text/javascript'>
    document.body.onload = function(){changeSize('added');};
    </script>
    </body>
    </html>

    This addition of eventHandler dynamically works in IE but not in mozilla.
    How to do it for mozilla
    In the above code 'added' is displayed in alert box on page load in IE, how to make it happen in mozilla.
     
    vikben, Nov 14, 2007 IP
  2. bonzay

    bonzay Peon

    Messages:
    54
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Use 'window.onload' instead of 'document.body.onload'.
    But changing 'window.onload' that way also removes all previously assigned window.onload event handlers... You may want to use a dedicated framework like jQuery, Prototype or YUI, which nicely allow adding event handlers dynamically.
     
    bonzay, Nov 14, 2007 IP
  3. vikben

    vikben Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    :)Thanks, Your reply solved my problem.
     
    vikben, Nov 15, 2007 IP