Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/414.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript domcontentloaded是否等待其他脚本完成加载?_Javascript_Html - Fatal编程技术网

Javascript domcontentloaded是否等待其他脚本完成加载?

Javascript domcontentloaded是否等待其他脚本完成加载?,javascript,html,Javascript,Html,index.html <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-

index.html

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    Hello world
    <script>
       print();
    </script>
    <script type="text/javascript" src="http://localhost:8080/static/js/index.js"></script>
           
</body>
</html>
未捕获引用错误:未定义打印它给了我这样的错误,因为index.js尚未到达。我可以这样做

//
<script type="text/javascript" src="http://localhost:8080/static/js/index.js"></script>
<script>
  print();
</script>
//
//
打印();
//
但在我的真实情况下,我不能那样做。我试着这样做:

//
 <script>
      document.addEventListener('DOMContentLoaded', ()=>{
        print();
    })
 </script>
//
//
document.addEventListener('DOMContentLoaded',()=>{
打印();
})
//

DOMContentLoaded事件是否会等待所有js文件到达?

这里的解决方案是:(a)不要使用内联脚本,在.js文件中编写js并像加载其他文件一样加载它们,不要在页面上直接使用带有js的裸
标记,以及(b)使用
加载脚本,以便在不阻塞页面的情况下加载它们,并且只在页面被解析后执行。自2013年底IE11问世以来,就不需要使用
DOMContentLoaded
事件侦听器(请注意,
async
defer
属性意味着您可以将所有内容放在
中,无需使用2012样式的“结束
”)或者只需将脚本库引用移动到关闭
正文
标记之前。这里的解决方案是:(a)不使用内联脚本,在.JS文件中编写JS并像加载其他文件一样加载它们,永远不要在页面上直接使用带有JS的裸
标记,以及(b)使用
加载脚本,以便在不阻塞页面的情况下加载脚本,并且仅在解析页面后执行脚本。自2013年底IE11问世以来,就不需要使用
DOMContentLoaded
事件侦听器(请注意,
async
defer
属性意味着您可以将所有内容放在
中,无需使用2012样式的“结束
”)或者只需将脚本库引用移动到关闭
body
标记之前。
//
 <script>
      document.addEventListener('DOMContentLoaded', ()=>{
        print();
    })
 </script>
//