简体   繁体   中英

Simple onload event not firing?

Just made a basic program to fin the root of my problem and I can't seem to find why this is not working at all.

HTML

<html lang="en">
    <head>
        <script type="text/javascript" src="carjava.js"></script>
    </head>
<body onload="hi()">
    hi
</body>

</html>

JS

function hi() {
alert("hi");
}

I want an alert to pop up when the page loads but nothing happens, any ideas?

I can't tell you why it isn't firing, but this should work:

HTML

<html lang="en">
    <head>
        <script type="text/javascript" src="carjava.js"></script>
    </head>
    <body>
        hi
    </body>
</html>

JS

function hi() {
    alert("hi");
}

window.onload = hi;

following code is working fine for me:

<html lang="en">
    <head>
        <script type="text/javascript" src="carjava.js"></script>
        <script type="text/javascript">
          function hi()
          {
             alert('hi');
          }
        </script>
    </head>
<body onload="hi()">
    hi
</body>

</html>

Its working fine in Firefox & Chrome. Must be something with js file.

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