简体   繁体   English

为什么外部 JS 无法启动?

[英]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!在进行一些研究之后,我采取了措施来保证在尝试运行 JS 之前加载它但是......它不会起作用!

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:测试.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.对于调用外部 js 文件的标记,您需要一个结束脚本标记。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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