HTML Scripts

HTML Scripts

A script is a small piece of a program embedded in web pages to make them dynamic and interactive. For example, with scripts, you can create a pop-up box message, a dropdown menu, etc. JavaScript is the most common scripting language used on websites. You can also use the <script> element with other languages (e.g. WebGL’s GLSl shading language).

How to Add Scripts

How to Add Scripts (如何添加脚本)

The <script> element is used to embed script or reference to an executable script within an HTML document. Scripts are usually nested within the <head> element to ensure that the script is ready to run when it is called. However, there is no restriction, and the script could be placed anywhere in the document.

If you need the same scripts to be available for many web pages, then you should place scripts into a separate file, then call it from the HTML document. (如果需要为许多网页提供相同的脚本,则应将脚本放入单独的文件中,然后从HTML文档调用它。)

You can insert the following piece of code with the <script> tag into your HTML code:

<script type="text/javascript" src="scripts.js"></script>

How to Trigger Scripts

How to Trigger Scripts (如何触发脚本)

You can specify whether to make a script run automatically (as soon as the page loads) or after the user has done something (like hovering over or clicking a link). If you want the script to run if the user performs any action (called event), then you should use event handlers to let the browser know, which event is responsible for triggering individual script. (您可以指定是让脚本自动运行(在页面加载后立即) ,还是在用户完成某些操作(如悬停或单击链接)后立即运行。如果希望在用户执行任何操作(称为事件)时运行脚本,则应使用事件处理程序告知浏览器,哪个事件负责触发单个脚本。)

Event handlers are specified as attributes within the HTML tag. (事件处理程序被指定为HTML标记中的属性。)

Example of the HTML <script> tag with an event handler:

<!DOCTYPE html>
<html>
 <head>
   <title>Event Handler Example</title>
   <script type="text/javascript">
     function myAlert() {
       alert("I am an event handler....");
     	 return;
     }
   </script>
 </head>
 <body>
   <span onmouseover="myAlert();">
   Bring your mouse here to see an alert
(将鼠标移至此处可查看提醒)
   </span>
 </body>
</html>

The <noscript> element

The <noscript> element

Though many modern browsers support JavaScript, there are some older browsers that cannot run a JavaScript code, and also there’s a probability that a user has disabled JavaScript on his/her browser. To provide alternative information for non-JavaScript browsers or browsers with JavaScript disabled, use the <noscript> tag. The contents of this tag are displayed to the user only in the case when JavaScript is disabled or when the browser doesn’t support JavaScript.

Example of the HTML <noscript> tag:

<!DOCTYPE html>
<html>
<head>
   <title>Title of the documnet</title>
 </head>
 <body>
   <script>
     document.write("My first JavaScript example!")
(document.write ("我的第一个JavaScript示例!"))
   </script>
   <noscript>Sorry, your browser does not support JavaScript!</noscript>
   <p>In case JavaScript is disabled or the browser doesn't support it, the code will display the content inside the noscript element.</p>
 </body>
</html>

Result



请遵守《互联网环境法规》文明发言,欢迎讨论问题
扫码反馈

扫一扫,反馈当前页面

咨询反馈
扫码关注
返回顶部