HTML Elements: <script>

By: Dusty Arlia
Published on August 1, 2012
Last Updated on Thursday, July 16, 2015 at 9:38 PM
Total Updates: 8

The <script> tag itself is HTML. It acts like a container which holds the script code. There are a couple different types of scripts. Within the <script> tag, you can specify the format and the type of script using the type attribute. For example, here's a <script> tag with the type attribute:

<script type="text/javascript">

text indicates the format, which in this case is just plain text. javascript indicates the type of script that follows. The browser will send the code following this <script> tag to the JavaScript interpreter.

The most popular type of script on the internet is JavaScript. When the browser sees the opening script tag (<script>) it knows that some sort of script is coming. You need to let the browser know that the script is JavaScript with the type attribute.

Note:

With HTML5, the default script format is JavaScript. So you don't have to include the type attribute if you're using HTML5. You can also choose to leave out the type attribute in HTML 4.01 and XHTML1.0 and the script will run the same. The problem with these two standards is that the page will not validate properly without this attribute.

When the browser sees the closing script tag (</script>), it knows it's reached the end of the JavaScript program and to start rendering HTML again.

You can insert JavaScript into your HTML document in different locations including both the <head> and <body> sections. You are also able to include multiple <script> tags.

Comments:

Ad: