How to use js in html5?

青灯夜游
Release: 2020-09-08 16:06:31
Original
6235 people have browsed it

In HTML5, you can embed JavaScript code directly in the page and include external JavaScript files. The syntax for embedded scripts is "<script></script>", and the syntax for external scripts It is "".

How to use js in html5?

In HTML5, js code can be used in two ways (embedded script and external script) through the <script> tag. The following article will introduce it to you, I hope it will be helpful to you. </p> <p><span style="font-size: 18px;"><strong>Two ways to use js in html5: </strong></span><br></p>##There are two ways to use the <script> tag: directly Embed JavaScript code in the page and include external JavaScript files; the browser will parse the <script> elements in the order they appear on the page. <p></p> <p>Embedded script: <strong></strong><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">&lt;script&gt; //js代码 &lt;/script&gt;</pre><div class="contentsignin">Copy after login</div></div></p><p>External script:<strong></strong><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">&lt;script src=&quot;外部js脚本文件&quot;&gt;&lt;/script&gt;</pre><div class="contentsignin">Copy after login</div></div></p><p>Advantages of external script: <strong></strong></p>● Maintainability<p></p>● Cacheable: The browser can cache all external JavaScript files linked to based on specific settings<p></p>● Adaptable to the future<p></p><p><span style="font-size: 18px;"><script> tag<strong></strong></span><br/></p>The main way to insert JavaScript into an HTML page is to use the <script> element. The <p></p><p><script> element has two commonly used attributes: <strong></strong><br/></p>● src: Indicates the external file containing the code to be executed. The src attribute is required if you want to include external JavaScript files through the <script> element. The value of this attribute is a link to an external JavaScript file. <p></p><p>Note: <strong>The <script> element with the src attribute should not contain additional JavaScript code between its <script> and <script> tags. If embedded code is included, only the external script file will be downloaded and executed, and the embedded code will be ignored. JavaScript files from external domains can also be included via the src attribute of the <script> element. </strong></p>Example: <p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">&lt;script src=&quot;example.js&quot;&gt;&lt;/script&gt; &lt;script src=&quot;//m.sbmmt.com/js/example.js&quot;&gt;&lt;/script&gt;</pre><div class="contentsignin">Copy after login</div></div></p>● Type: Specifies the MIME type of the script. The type attribute indicates the content between the <script> and </script> tags.

Description: MIME type includes two parts: media type and subtype. For JavaScript, the MIME type is "text/javascript".

Note: In HTML5, the type attribute is no longer required. The default value is "text/javascript". JavaScript is the default scripting language in HTML5 and all modern browsers.

Recommended video tutorials: "

HTML5 Tutorial", "JavaScript Tutorial"

##

The above is the detailed content of How to use js in html5?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template