Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use JavaScript in HTML

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article focuses on "how to use JavaScript in HTML". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let the editor take you to learn how to use JavaScript in HTML.

Label

In HTML5, script mainly has the following attributes: async,defer,charset,src,type

Async (optional):

Keywords: asynchronous scripts, external files, download immediately

When this attribute is included in the tag, the script (external file) is downloaded immediately, which is only valid for external script files. Other operations on the page can be performed at the same time. After the download is completed, the parsing is stopped and executed, and the parsing continues after execution, but the execution order is not guaranteed.

Defer (optional):

Keywords: deferred scripts, external files, delayed loading

When this attribute is included in the tag, the script can wait until the page is fully parsed or displayed and is only valid for external files. If there are two scripts with defer at the same time, the former will be limited to the latter due to delay.

Charset (optional):

Key words: character set

Most browsers have ignored its value, so few people use it.

Src (optional):

Key words: external reference

Represents the address of the external file to be referenced.

Type (optional):

Keywords: MIME (content type of scripting language)

To ensure maximum browser compatibility, the property value of type is still mainly text/javascript. If this property is not written, the default value is still text/javascript.

Note: when referencing external files, do not add other JS code to the tag. When parsing, the browser will only download the external script files referenced by src, and the code embedded in the table will be ignored.

Location of the label

Usually, we put references to tags with external files (including CSS files, JavaScript files) in the same place, usually inside the tag.

But once multiple JavaScript external files are encountered in the parsing process, the page will not be fully displayed until all the external files have been loaded, so we usually put it at the bottom of the tag.

The attribute defer is mentioned above, but as mentioned in HTML5, HTML5 will ignore the defer attribute set by the embedded script, currently only IE4~IE7 supports the defer attribute, and IE8 fully follows the HTML5 standard in the future, so putting the bottom in the tag is still the best choice.

Advantages of referencing external files

Easy to maintain: put all JavaScript files together, not only can not touch the HTML code, but also more conducive to developers to write and maintain the code.

Speed up browsing: if there are multiple HTML pages referencing the same JavaScript external file, the file will only be loaded once (cache), which means that the page can be loaded faster.

Security: referencing external files, if users look at the HTML code, they will not see the JavaScript code, which is more secure than writing it in the tag.

Label

Literally, NO-script, without script, that is, when the browser does not support JavaScript, the content in the tag will be displayed.

Browsers do not support scripts

Browsers support scripts, but JavaScript is disabled

If either of the above two items matches, the contents of the tag will be displayed.

The above page gives the user a message that will only be displayed when the browser does not support or disable JavaScript, otherwise the user will never see it and will not affect the display of other elements of the page.

At this point, I believe you have a deeper understanding of "how to use JavaScript in HTML". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report