HTML Old School

SCRIPT tag

Script tag is a container for writeing JavaScript code.

This element is not specific only for HEAd because we can put it inside BODY too.

 

HTML 4 example:

<script type="text/javascript" src="jquery.min.js"></script>
 
<script type="text/javascript">
   document.write("Hello World!");
</script>

<script type="text/javascript"><!--
   document.write("Hello World!");
--></script>

 

 

HTML 5 example:

<script src="jquery.min.js"></script>
 
<script><!--
   document.write("Hello World!");
--></script>

 

Sometimes it is good put JS code inside HTML comment. This prevents showing code in older browsers.

Example with html comment inside script tag: 05script_comment.html

 

 

 

Script tag attributes

1. type="text/javascript"

Not required in HTML5.

 

2. src="URI"

Defines path to the javascript file.

 

3. charset="windows-1250"

Defines charset of external Javascript file. It is very usefull when we want to integrate for example windows-1250 js file into utf-8 HTML document.

JS file is written in windows-1250, and HTML doc in UTF-8, but everything works OK: 05script_charset.html

 

4. defer

Say to browser not to load JS file unless all HTML elements are not loaded. So first will be loaded all HTML element, and atthe end javascript file.

<script src="jquery.min.js" defer></script>

 

5. async

Load javascript file while HTML elements are loading e.g. HTML elements and JS file are loading at the same time.

<script src="jquery.min.js" async></script>

 

Notice: If defer nor async are written then JS file is loaded before HTML elements (when script tag is inside HEAd tag).