Google Chrome loads the javascript first even I put the script tag before the end of the body tag

后端 未结 5 1225
刺人心
刺人心 2021-02-11 06:19


I have a problem with loading JavaScript in Google Chrome.
I\'ve created the separate js file with a simple alert message and then linked it before the end of the body

5条回答
  •  温柔的废话
    2021-02-11 06:37

    Your problem is that your script have the tag async, which let it execute whitout taking care of the web page loading state. Remove the async tag, or replace it with defer, which execute the script after the page loading.

    In order to prevent any problem with script and html/css loading times conflict, you should encapsulate your Javascript's scripts with window.onload = function() { //code here }. This will guarantee that your whole page is loaded before executing your code.

提交回复
热议问题