简体   繁体   中英

javascript event handling not working

I am following this link to learn about basic javascript but for some strange reason this little piece of code does not seem to be playing nice with me. Any suggestions on why I am not getting the alerts would be appreciated

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8" />
        <title>Creeper</title>
        <script type="text/javascript">
            addEvent(window, "load", sayHi);
            function sayHi(){
                alert("jekyl");
            }
        </script>
    </head>
    <body>
        <a href="#" title="click me", id="my-link">Click Me!</a>
    </body>
</html>

addEvent isn't a method implemented in window . The article you're following refers to it, but is assuming you've loaded an implementation first, which you haven't done in the code you posted. You can try Dean Edwards implementation (link in the comments above) or try this:

   <script type="text/javascript">
        window.onload = function() {
            alert("jekyl");
        }

    </script>

Also you can use Event Listeners:

if (window.addEventListener){
    window.addEventListener('load', sayHi, false); // All browsers except IE
} else if (window.attachEvent && !window.opera){
    window.attachEvent('onload', sayHi); // IE
}

function sayHi(){
    alert("jekyl");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM