The HTML Document
An html document is made up of html elements, html element attributes, comments, special characters and doctype. If you like to add presentational features to an html document you can attach css to an html document, to add dynamic user experience (e.g. popup, alert message, animations etc.) to an html document you can add JavaScipt to your html document.
If javascript is disabled in the browser property, even though an external script is attached or a script is written within <script>...</script> tags in an html document, it becomes inactive.
Certain JavaScripts do not work with all the browsers and sometimes a script works on and above (or vice a versa) a particular version of a web browser.
The script Tag
The script is an HTML element. Html script element is used to enclose client side scripts like JavaScript within an html document.
Syntax
<script> JavaScript statements....... </script>
There are four type of attributes in script element:
There are four type of attributes in script element:
1. language
The language attribute is used to specify the scripting language and it's version for the enclosed code. In the following example JavaScript version is 1.2. If a specific browser does not support the said JavaScript version, the code is ignored. If you do not specify a language attribute, the default behavior depends on the browser version. The language attribute is deprecated in HTML 4.01.
Example
HTML Code
- <script language = "JavaScript1.2">
- JavaScript statements.......
- </script>
2. src
This attribute specifies the location of an external script. This attribute is useful for sharing functions among many different pages. Note that external JavaScript files contain only JavaScript statements and files must have the extension .js.
Example
HTML Code
- <script src = "common.js">
- JavaScript statements.......
- </script>
3. defer
If you set defer attribute, the browser delays the execution of the script or it changes the order of the execution of the script. This can improve the performance by delaying execution of scripts until the content of body is read and displayed by browser.
4. type
This attribute specifies the scripting language. The scripting language is specified as a content type (e.g., "text/javascript" ). The attribute is supported by all modern browser.
Example
HTML Code
- <script type="text/javascript">
- JavaScript statements.......
- </script>
The noscript tag
If any browser does not support the JavaScript code the alternate content placed within noscript tag is being executed.
Example
HTML Code
- <noscript>
- ... code ....
- </noscript>
Javascript in html document
There are two general areas in HTML document where JavaScript can be placed. First is between <head>......</head> section, another is specific location in<body>......</body> section. If you want to display a message 'Good Morning' (through the JavaScript alert command) at the time of page loading then you must place the script at the <head>......</head> section. In the following examples you will see the different location of <script>.....</script> tags in a HTML document.
Script in the Head
HTML Code
- <!DOCTYPE html>
- <head>
- <meta charset="utf-8" />
- <title> Script in head section </title>
- <script type = "text/javascript">
- JavaScript statements.......
- </script>
- </head>
- <body>
- </body>
- </html>
Script in the Body
HTML Code
- <!DOCTYPE html>
- <head>
- <meta charset="utf-8" />
- <title> Script in the Body </title>
- </head>
- <body>
- <script type = "text/javascript">
- JavaScript statements.......
- </script>
- </body>
- </html>
Scripts in the Head and Body
HTML Code
- <!DOCTYPE html>
- <head>
- <meta charset="utf-8" />
- <title> Script in head and body section </title>
- <script type = "text/javascript">
- JavaScript statements.......
- </script>
- </head>
- <body>
- <script type = "text/javascript">
- JavaScript statements.......
- </script>
- </body>
- </html>
Two Scripts in the Body
HTML Code
- <!DOCTYPE html>
- <head>
- <meta charset="utf-8" />
- <title> Two Scripts in the Body </title>
- </head>
- <body>
- <script type = "text/javascript" scr="jsexample.js" >
- </script>
- <script type = "text/javascript">
- JavaScript statements.......
- </script>
- </body>
- </html>
Note : Optionally, if your script is not required to be executed before the content of the body is displayed, and your script takes longer time to load objects, you can place your script at the end of the body element.
Please Google+, Like this tutorial on FaceBook, Tweet, save it as bookmark and subscribe with our Feed. Have suggestions? comment using Disqus down this page. Thanks.
No comments:
Post a Comment