HTML脚本标签教程和示例

HTML <script> is a tag used to put some executable code which is named as a script into the web age or HTML code. <script> tag will contain different scripts that can be JavaScript or WebGL GLSL programming language or provide a reference to external scripts to be loaded and executed.

HTML <script>是用于将一些称为脚本的可执行代码放入网络时代或HTML代码的标记。 <script>标记将包含不同的脚本,这些脚本可以是JavaScript或WebGL GLSL编程语言,或者提供对要加载和执行的外部脚本的引用。

<script>标签和JavaScript (<script> Tag and JavaScript)

<script> is a tag that starts the script block and the script block is ended with the </script> enclosing tag. The block inside these tags will be referred to as script body or block and may contain JavaScript, WebGL GLSL or reference to the external script files which can be server from the current domain or external domain. But in general script tag is used to run JavaScript code which is very popular even defacto scripting language for web pages.

<script>是开始脚本块的标记,而脚本块以</ script>封闭标记结束。 这些标记内的块将称为脚本主体或块,并且可能包含JavaScript,WebGL GLSL或对外部脚本文件的引用,这些文件可以是当前域或外部域中的服务器。 但是通常,脚本标签用于运行JavaScript代码,该代码非常流行,甚至是网页的事实上的脚本语言。

<script>
  alert("This is JavaScript");
</script>

运行脚本代码 (Run Script Code)

The script tag is used to run JavaScript code where the code will be put into the script tags. The given code will be executed immediately without waiting for a specific event. In the following example, we will show a message box with a message.

脚本标签用于运行JavaScript代码,并将代码放入脚本标签中。 给定的代码将立即执行,而无需等待特定事件。 在下面的示例中,我们将显示一个带有消息的消息框。

<html>
   <body>
      <h1>The HTML Script Tag Example</h1>
      <script>
         alert("I like poftut.com");   
      </script>
   </body>
</html>

设置脚本类型 (Set Script Type)

Even there are multiple scripting languages that can be used with the script tag JavaScript is the default scripting language that is used with script implicitly. Alternatively, we can specify the scripting language with the type attribute of the script tag. In the following example, we will put two script block where first is JavaScript which value is text/javascript and the second block is WebGL which value is x-shader/x-vertex.

即使脚本标记可以使用多种脚本语言,JavaScript也是隐式地与脚本一起使用的默认脚本语言。 另外,我们可以使用script标签的type属性指定脚本语言。 在下面的示例中,我们将放置两个脚本块,其中第一个是JavaScript,其值为text/javascript ,第二个是WebGL,其值为x-shader/x-vertex

<html>
   <body>
      <h1>The HTML Script Tag Example</h1>
      <script type="text/javascript">
         alert("I like poftut.com");
      </script>
      <script type="x-shader/x-vertex">
         attribute vec2 aVertexPosition;
      </script>
   </body>
</html>

指定外部脚本文件 (Specify External Script File)

Script tag can also load and run an external script file. The script file can be on the same domain or on the other domain. The script file path or URI will be specified with the attribute src.

脚本标记还可以加载和运行外部脚本文件。 脚本文件可以在同一域上,也可以在其他域上。 脚本文件路径或URI将使用属性src指定。

<html>
   <body>
      <h1>The HTML Script Tag Example</h1>
      <script src="poftut.js"></script>
      <script src="../scripts/poftut.js"></script>
      <script src="https://www.poftut.com/scripts/my.js"></script>
   </body>
</html>

异步运行脚本 (Run Script Asynchronized)

By default, the specified script block of the script file is loaded as soon as possible. But in some cases using lazy loading can be more beneficial which is called asynchronized loading. This will iterate the loading of the script and will create less load for the web client or web browser. Lazy loading generally used with external script files and the attribute async is used. All the external script files will be loaded synchronized in the following example.

默认情况下,将尽快加载脚本文件中指定的脚本块。 但是在某些情况下,使用延迟加载会更有利,这称为异步加载。 这将迭代脚本的加载,并为Web客户端或Web浏览器创建较少的负载。 延迟加载通常与外部脚本文件一起使用,并且使用属性async 。 在以下示例中,将同步加载所有外部脚本文件。

<html>
   <body>
      <h1>The HTML Script Tag Example</h1>
      <script src="poftut.js" async></script>
      <script async src="../scripts/poftut.js"></script>
      <script async src="https://www.poftut.com/scripts/my.js"></script>
   </body>
</html>

设置外部脚本的字符集/编码 (Set Character Set/Encoding For External Script)

By default, the current HTML document character set or encoding is used for the script block. In some cases, the encoding of the script block especially external script files can be different than the current HTML document.

默认情况下,当前HTML文档字符集或编码用于脚本块。 在某些情况下,脚本块(尤其是外部脚本文件)的编码可能不同于当前HTML文档。

<html>
   <body>
      <h1>The HTML Script Tag Example</h1>
      <script charset="UTF-8" src="poftut.js"></script>
      <script charset="ISO-8859-1" src="../scripts/poftut.js"></script>
      <script src="https://www.poftut.com/scripts/my.js" charset="UTF-8"></script>
   </body>
</html>

页面解析后执行脚本 (Execute Script After The Page Parsing)

Even async provides lazy loading there is an alternative way to load external scripts that are called as defer attribute. With the defer attribute the given external script will be loaded after the page parsing which means after all the elements of the HTML document or web page are parsed and put properly. Defer attributes can be only used for the external script files.

即使异步提供了延迟加载,也有另一种方法来加载称为defer属性的外部脚本。 使用defer属性,给定的外部脚本将在页面解析后加载,这意味着在HTML文档或网页的所有元素都被解析并正确放置之后。 延迟属性只能用于外部脚本文件。

<html>
   <body>
      <h1>The HTML Script Tag Example</h1>
      <script src="poftut.js" defer></script>
      <script defer src="../scripts/poftut.js"></script>
      <script defer src="https://www.poftut.com/scripts/my.js"></script>
   </body>
</html>
LEARN MORE  HTML Div Tag Usage Tutorial with Examples
了解更多有关HTML Div标签用法的示例教程

翻译自: https://www.poftut.com/html-tag-tutorial-with-examples/

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值