JavaScript – Script Tag, Attributes, and Syntax

JavaScript Tag, Attribute & Syntax

The <script> tag is used to define the JavaScript code. It identifies the block of the script on the page.

The <script> tag loads the script file which has extensions [eg. .js, script.js], or the script code written inside the <script> tag.

Attributes of <script> Tag

AttributeValueDescription
typemedia_typeIt specifies the media type of the script
srcurlIt specifies the URL of the external file of JavaScript in the form of .js extension.
deferdeferSpecifies that the script is executed when the page has finished parsing support external script files.
charsetcharsetCharacter encoding used in an external file of JavaScript.
XML:spacepreserveWhite space in code should be preserved
asynasyncSpecifies the script is executed asynchronously supports for external files.

The External JavaScript 

The external JavaScript file is embedded like as following in a web page.

<script src="script.js"></script>

HTML / JS Code

<html>
 <head>
  <script type="text/javascript">
   function display(){
   alert ("Hello!");
   }
  </script>
 </head>
 <body>
  <input type="button" onClick="display()" value="How is That?">
 </body> 
</html>

TutsMaster.org

Tutsmaster.org provides tutorials related to tech and programmings. We are also setting up a community for the users and students.