Javascript IE Event

10,435

Solution 1

To get the target of an event in both standards compliant browsers and IE, use

var target = evt ? evt.target : window.event.srcElement;

There's an overview of the different properties of event objects at MDC.

Solution 2

  • When does this script run? You might have to run this script onload, after the DOM is fully loaded
<script>

function go()
{
  alert('dom is loaded:  register event handlers now') ;
}

</script>

<body onload=" go(); ">



</body>

Solution 3

As mentioned, IE does not pass the event object as a parameter. Try this:

var form = document.getElementById('theform')
  /* create the event handler */
  form.gen.onclick = function( evt ) {
    if(!evt)
      evt = window.event;
    var f = evt.target.form
    var y = f.year.value
    var m = f.month.value
    genCalendar( document, y, m, 'theCalendar' )
}

Or better yet, use a cross-browser library, like Prototype.js or jQuery.

Share:
10,435
Admin
Author by

Admin

Updated on June 04, 2022

Comments

  • Admin
    Admin almost 2 years

    This works in Firefox, but not IE. Any help would be much appreciated! Thanks!

      var form = document.getElementById('theform')
        /* create the event handler */
        form.gen.onclick = function( evt ) {
            var f = evt.target.form
            var y = f.year.value
            var m = f.month.value
            genCalendar( document, y, m, 'theCalendar' )
        }
    
  • Christoph
    Christoph almost 15 years
    also, you'll have to use .srcElement instead of .target in IE
  • banana
    banana about 9 years
    I think it should be: var target = evt.target ? evt.target : window.event.srcElement;