Home>Article>Web Front-end> How to use JavaScript in HTML
Recommended learning:In HTML, you can use javascript in the script tag, just add the "" code to the page. The script tag is used to define client scripts. This element can either contain script statements or point to it through the src attribute. version, Dell G3 computer.
html video tutorial
1. Use the script element
HTML 4.01 defines 6 attributes for 3f1c4e4b6b16bbbd69b2ee476dc4f83a
async (optional)//异步脚本 //指定async属性的目的是不让页面等待这两个脚本下载和执行,从而异步加载页面其他内容。 //但是不保证它们的先后顺序,也就是第二个脚本可能在第一个脚本之前执行。
charset (optional, rarely used)
Specifies the character value of the code, most browsers will Ignore, rarely usedindicates that the script can be delayed until the document is completely parsed and displayed. Only valid for external files.
//延迟脚本 //添加defer属性之后,直到遇到才会执行,也就是最后最后执行就是它了
language (obsolete)
Represents the external file containing the code to be executed
//外部文件first.js将被加载到当前页面中 //注意注意呀,此时在之间不再包含额外的JavaScript代码,因为这些额外的代码将会被忽略,不执行!!!
//也就是说src还可以包含来自外部域的.js文件,而不仅仅是自己编写的。功能更加强大。 //注意注意呀,外部域的.js文件可能不可控,所以要么你是它的所有者、要么他的所有者值得信赖、要么就不要使用!!!
type (optional)
2. Tag position
Best Practice
Place all JavaScript references after the content of the 6c04bd5ca3fcae76e30b72ad730ca86d element
位置很重要
Of course there is no problem embedding JavaScript in html. But the best practice is to use external files to contain JavaScript code whenever possible. It has the following advantages:
If two pages use the same .js file, it will only be downloaded once to speed up page loading
The above is the detailed content of How to use JavaScript in HTML. For more information, please follow other related articles on the PHP Chinese website!