迹忆客 专注技术分享

当前位置:主页 > 学无止境 > WEB前端 > JavaScript >

HTML Script Type 属性的使用

作者:迹忆客 最近更新:2024/03/19 浏览次数:

本教程介绍如何使用 HTML 脚本 type 属性。你也可以将其称为 JavaScript MIME(多用途 Internet 邮件扩展)type

在此之前,有必要知道使用 type 属性的原因。它表明文档的格式和性质。

我们也可以说它代表脚本的类型,application/javascript(默认值)或 application/ecmascript。你可以检查 this 了解更多媒体类型。

我们了解脚本的 type 属性以及为什么要使用它,但是在哪里呢?当我们使用现代浏览器时,我们不需要在 <script> 标记中使用 type 属性,因为它们默认具有它。

此外,如果你使用的是 HTML5,那么你可以简单地编写 <script> ... </script>,因为它具有可选的 type 属性和默认的 text/javascript

在另一种情况下,你必须在使用 HTML 4.01 和 XHTML 1.0 时指定 type 属性,因为两者都需要它。

如果内容不被解释为 JavaScript,例如 VBScripts,你还必须使用 type 属性。

你可以利用以下启动代码来练习每种可能的情况。


在 HTML5 中使用 HTML 脚本 type 属性

对于 HTML5,不需要在 <script> 标记中使用 type 属性。请参阅以下示例代码。

<!DOCTYPE html>
<html>
	<head>
 		<title>JavaScript MIME Type</title>
 		<script>
 			console.log("Hello JavaScript MIME Type");
 		</script>
 	</head>
 <body>
 	<h1>Let's learn the use of JavaScript MIME type.</h1>
 </body>
</html>

在 HTML 4.01、XHTML 1.0 和 VBScript 中使用 HTML 脚本 type 属性

使用现代浏览器,你可以在编写 HTML 4.01 和 XHTML 1.0 时使用没有 type 属性的 <script> 标记。但是最好使用 type 属性,因为两者都是 require。

如果你在使用 VBScripts 时还使用了 type 属性,将会有所帮助。查看以下代码片段。

HTML 4.01 代码:

<!"-//W3C//DTD HTML 4.01//EN"
 "https://www.w3.org/TR/html4/strict.dtd">

<html>
 	<head>
 		<title>JavaScript MIME Type</title>
 		<script type="text/javascript">
 			console.log("Hello JavaScript MIME Type");
 		</script>
 	</head>
 <body>
 	<h1>Let's learn the use of JavaScript MIME type.</h1>
 </body>
</html>

XHTML 1.0 代码:

<! "-//W3C//DTD XHTML 1.0 Strict//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html>
 	<head>
 		<title>JavaScript MIME Type</title>
 		<script type="text/javascript">
 			console.log("Hello JavaScript MIME Type");
 		</script>
 	</head>
 <body>
 	<h1>Let's learn the use of JavaScript MIME type.</h1>
 </body>
</html>

带有 VBScript 的 HTML 4.01:

<! "-//W3C//DTD HTML 4.01//EN"
 "https://www.w3.org/TR/html4/strict.dtd">

<html>
 	<head>
 		<title>A document with SCRIPT</title>
 		<script type="text/vbscript" src="http://someplace.com/progs/vbcalc">
 		</script>
 	</head>
 <body>
 	<h1>Let's learn the use of JavaScript MIME type.</h1>
 	<script type="text/javascript">
 		console.log("HELLO VBScript")
 	</script>
 </body>
</html>

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Do you understand JavaScript closures?

发布时间:2025/02/21 浏览次数:108 分类:JavaScript

The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.

Do you know about the hidden traps in variables in JavaScript?

发布时间:2025/02/21 浏览次数:178 分类:JavaScript

Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av

How much do you know about the Prototype Chain?

发布时间:2025/02/21 浏览次数:150 分类:JavaScript

The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start

用 jQuery 检查复选框是否被选中

发布时间:2024/03/24 浏览次数:102 分类:JavaScript

在本教程中学习 jQuery 检查复选框是否被选中的所有很酷的方法。我们展示了使用直接 DOM 操作、提取 JavaScript 属性的 jQuery 方法以及使用 jQuery 选择器的不同方法。你还将找到许多有用的

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便