简体   繁体   中英

Why external JS won't start?

after to make some research I took the measures to guarantee that the JS will be loaded before trying to run it but... it won't work!

This works:

<!DOCTYPE html>
<head>
    <meta name="viewport" content="initial-scale=1" charset="utf-8">
</head>
<html>
    <body>
    <script type="text/javascript">
        function initialize() {
            console.log('foo')
        }

        window.onload = function() {
            initialize();
        };
    </script>
</body>
</html>

But this doesn't:

<!DOCTYPE html>
<head>
    <meta name="viewport" content="initial-scale=1" charset="utf-8">
</head>
<html>
    <body>
    <script type="text/javascript" src="test.js">
    <script type="text/javascript">
        window.onload = function() {
            initialize();
        };
    </script>
</body>
</html>

test.js:

function initialize() {
    console.log('foo')
}

I don't get any error. It just doesn't work. What am I doing wrong?

Thanks!

You need a closing script tag for the tag that is calling the external js file.

<script type="text/javascript" src="test.js"></script>

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