js external file into the html file in question fail

Failure is a matter of time because the js file loaded, if before the page is loaded, that is, the introduction of js files directly in the head, nor do other statements, it will lead to failure js files, because many js operations to the page for the elements to do so before loading the page loads the js is useless.

Solution:

1, in js file, the need to use js code into window.onload = function () {} function go of such, which js code will default to run after the page is loaded, it will not if needed elements can not find it;

2, is directly written in the body or introduced js file js file, i.e., the need to use js code or file is written into or behind those of the corresponding element, these elements can be avoided in the case of not found;

Guess you like

Origin www.cnblogs.com/lxl87/p/10936468.html