English 中文(简体)
HTML - Javascript
  • 时间:2024-09-17

HTML - JavaScript


Previous Page Next Page  

A script is a small piece of program that can add interactivity to your website. For example, a script could generate a pop-up alert box message, or provide a dropdown menu. This script could be written using JavaScript or VBScript.

You can write various small functions, called event handlers using any of the scripting language and then you can trigger those functions using HTML attributes.

Now-a-days, only JavaScript and associated frameworks are being used by most of the web developers, VBScript is not even supported by various major browsers.

You can keep JavaScript code in a separate file and then include it wherever it s needed, or you can define functionapty inside HTML document itself. Let s see both the cases one by one with suitable examples.

External JavaScript

If you are going to define a functionapty which will be used in various HTML documents then it s better to keep that functionapty in a separate JavaScript file and then include that file in your HTML documents. A JavaScript file will have extension as .js and it will be included in HTML files using <script> tag.

Example

Consider we define a small function using JavaScript in script.js which has following code −

function Hello() {
   alert("Hello, World");
}

Now let s make use of the above external JavaScript file in our following HTML document −

<!DOCTYPE html>
<html>

   <head>
      <title>Javascript External Script</title>
      <script src = "/html/script.js" type = "text/javascript"/></script>
   </head>

   <body>
      <input type = "button" oncpck = "Hello();" name = "ok" value = "Cpck Me" />
   </body>

</html>

This will produce the following result, where you can try to cpck on the given button −