简体   繁体   English

如果我链接外部javascript,在dom完全加载后是否可以运行?

[英]If I link external javascript, does it run after the dom is fully loaded?

Do I have to use a callback like this: 我必须使用这样的回调:

<body onload="myFunction()">

or can I assume that if I use an external file for javascript that I am safe? 还是可以假设如果我将外部文件用于javascript是安全的?

There is also 也有

<script onload="">

And as suggested in comment on op post. 并在op post的评论中建议。

<html>
  <head>
    <script onload="">
  </head>
  <body onload="">
    <!-- Dom here -->
    <script onload="">
  </body>
</html>

The first script MIGHT be loaded before dom is loaded. 在加载dom之前,可能会加载第一个脚本。 (depending filesize and dom complexity) On the second script tag the script will start loading after the dom is there. (取决于文件大小和dom复杂度)在第二个script标记上,dom在那里之后,脚本将开始加载。

jQuery $(document).ready() (.ready)可用于运行应在dom完全加载后执行的代码。

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

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