Javascript if (typeof(window.event) != "undefined") { problem

Discussion in 'Scripts' started by benjamin.morgan, Oct 12, 2011.

to remove all ads.
  1. benjamin.morgan

    benjamin.morgan New Member

    Messages:
    269
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head>
    <title>Simple Event Example</title>
    <script type="text/javascript">
    function addEventHandler(oNode, sEvt, fFunc, bCaptures){
    if (typeof(window.event) != "undefined") {
    oNode.attachEvent("on" + sEvt, fFunc);
    }else{
    oNode.addEventListener(sEvt, fFunc, bCaptures);
    }
    }

    function onLinkClicked(e) {
    alert("You clicked the link");
    }

    function setUpClickHandler() {
    addEventHandler(document.getElementById('clickLink'), "click", onLinkClicked, false);
    }
    addEventHandler(window,"load", setUpClickHandler, false);
    </script>
    </head>
    <body>
    <a href="#" title="click me" id="clickLink">Click Me!</a>
    </body>
    </html>
    I got this out of a tutorial.
    This works fine in FIREFOX. In CHROME and INTERNET EXPLORER 9 this doesn't work.
    Please help me find the fix.
     
  2. *EA*Marcus

    *EA*Marcus New Member

    Messages:
    5
    Answer

    Replace:
    With:
    That should work, it's a common problem.
     

Share This Page