JavaScript code" code to the page. The script tag is used to define client scripts. This element can either contain script statements or be pointed to by the src attribute."/> JavaScript code" code to the page. The script tag is used to define client scripts. This element can either contain script statements or be pointed to by the src attribute.">

Home>Article>Web Front-end> How to use JavaScript in HTML

How to use JavaScript in HTML

醉折花枝作酒筹
醉折花枝作酒筹 Original
2021-06-15 11:37:57 2929browse

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.

Recommended learning:

html video tutorial

1. Use the script element

HTML 4.01 defines 6 attributes for 3f1c4e4b6b16bbbd69b2ee476dc4f83a

async (optional)

indicates that the script should be downloaded immediately, but does not hinder other operations on the page, such as downloading other resources or other scripts to be loaded. Only valid for external script files

//异步脚本   //指定async属性的目的是不让页面等待这两个脚本下载和执行,从而异步加载页面其他内容。 //但是不保证它们的先后顺序,也就是第二个脚本可能在第一个脚本之前执行。
  • charset (optional, rarely used)

    Specifies the character value of the code, most browsers will Ignore, rarely used
  • #defer (optional)

    indicates that the script can be delayed until the document is completely parsed and displayed. Only valid for external files.

  • //延迟脚本   //添加defer属性之后,直到遇到才会执行,也就是最后最后执行就是它了

  • language (obsolete)

    Used to indicate the programming language used to write code, obsolete
  • src (optional)

    Represents the external file containing the code to be executed

  •  //外部文件first.js将被加载到当前页面中 //注意注意呀,此时在之间不再包含额外的JavaScript代码,因为这些额外的代码将会被忽略,不执行!!!
     //也就是说src还可以包含来自外部域的.js文件,而不仅仅是自己编写的。功能更加强大。 //注意注意呀,外部域的.js文件可能不可控,所以要么你是它的所有者、要么他的所有者值得信赖、要么就不要使用!!!

  • type (optional)

    can be regarded as language Substitutes, commonly used are type=text/javascript, which do not have to be
Included in 3f1c4e4b6b16bbbd69b2ee476dc4f83a2cacc6d41bbb37262a98f745aa00fbf0 will be explained from top to bottom. No other content on the page will be loaded and displayed until all code inside the interpreted 3f1c4e4b6b16bbbd69b2ee476dc4f83a element has been evaluated.

  • 2. Tag position

  • Traditional approach
All 3f1c4e4b6b16bbbd69b2ee476dc4f83a are placed in 93f0f5c25f18dab9d176bd4f6de5d30e for the purpose Put all external files .css and .js in the same place.

But this will cause the page content in 6c04bd5ca3fcae76e30b72ad730ca86d to not be rendered before all the JavaScript code has been downloaded and interpreted. Oh my god, what if there are a lot of JavaScript codes? ! ! It will look like the browser window is blank at first glance. What is this?

  • Best Practice
    Place all JavaScript references after the content of the 6c04bd5ca3fcae76e30b72ad730ca86d element

  •     位置很重要 
    
  • 3. Selection of embedded code or external files


    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:

Maintainability

It is much easier to maintain the .js file in a folder without touching the HTML tags

  • Cacheable

    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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn