- HTML Tutorial
- HTML - Home
- HTML - History and Evolution
- HTML - Overview
- HTML - Editors
- HTML - Basic Tags
- HTML - Elements
- HTML - Attributes
- HTML - Formatting
- HTML - Headings
- HTML - Paragraphs
- HTML - Quotations
- HTML - Comments
- HTML - Phrase Tags
- HTML - Meta Tags
- HTML - Style Sheet
- HTML - CSS Classes
- HTML - CSS IDs
- HTML - Images
- HTML - Image Map
- HTML Tables
- HTML - Tables
- HTML - Headers & Caption
- HTML - Table Styling
- HTML - Table Colgroup
- HTML - Nested Tables
- HTML Lists
- HTML - Lists
- HTML - Unordered Lists
- HTML - Ordered Lists
- HTML - Definition Lists
- HTML Links
- HTML - Text Links
- HTML - Image Links
- HTML - Email Links
- HTML - Iframes
- HTML - Blocks
- HTML Backgrounds
- HTML - Backgrounds
- HTML Colors
- HTML - Colors
- HTML - RGB
- HTML - HEX
- HTML - HSL
- HTML Forms
- HTML - Forms
- HTML - Form Attributes
- HTML - Form Control
- HTML - Input Attributes
- HTML Media
- HTML - Video Element
- HTML - Audio Element
- HTML - Embed Multimedia
- HTML Header
- HTML - Head Element
- HTML - Adding Favicon
- HTML - Javascript
- HTML Layouts
- HTML - Layouts
- HTML - Layout Elements
- HTML - Layout using CSS
- HTML - Responsiveness
- HTML - Symbols
- HTML - Emojis
- HTML - Style Guide
- HTML Graphics
- HTML - SVG
- HTML - Canvas
- HTML APIs
- HTML - Geolocation API
- HTML - Drag & Drop API
- HTML - Web Workers API
- HTML - WebSocket
- HTML - Web Storage
- HTML - Server Sent Events
- HTML Miscellaneous
- HTML - MathML
- HTML - Microdata
- HTML - IndexedDB
- HTML - Web Messaging
- HTML - Web CORS
- HTML - Web RTC
- HTML Demo
- HTML - Audio Player
- HTML - Video Player
- HTML - Web slide Desk
- HTML Tools
- HTML - Velocity Draw
- HTML - QR Code
- HTML - Modernizer
- HTML - Validation
- HTML - Color Code Builder
- HTML References
- HTML - Tags Reference
- HTML - Attributes Reference
- HTML - Events Reference
- HTML - Fonts Reference
- HTML - ASCII Codes
- ASCII Table Lookup
- HTML - Color Names
- HTML - Entities
- MIME Media Types
- HTML - URL Encoding
- Language ISO Codes
- HTML - Character Encodings
- HTML - Deprecated Tags
- HTML Resources
- HTML - Quick Guide
- HTML - Useful Resources
- HTML - Color Code Builder
- HTML - Online Editor
HTML Scripts
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 Javascript or VBScript.
You can write your Event Handlers using any of the scripting language and then you can trigger those functions using HTML attributes.
There are two ways of using a style sheet in an HTML document:
External Script:
If you have to use a single script functionality among many HTML pages then it is a good idea to keep that function in a single script file and then include this file in all the HTML pages. You can incluse a style sheet file into HTML document using <script> element. Below is an example:
<head> <script src="yourfile.js" type="text/javascript" /> </head> |
Internal Script:
You can write your script code directly into your HTML document. Usually we keep script code in header of the document using <script> tag, otherwise there is no restriction and you can put your source code anywhere in the document. You can specify whether to make a script run automatically (as soon as the page loads), or after the user has done something (like click on a link). Below is an example this would write a Hello Javascript! message as soon as the page loads.:
<head> <title>Internal Script</title> </head> <body> <script type="text/javascript"> document.write("Hello Javascript!") </script> </body> |
This will produce following result:
To become more comfortable - Do Online Practice
Writing Event Handler:
It is very easy to write an event handler. Following example explains how to write an event handler. Let's write one simple function myAlert in the header of the document. We will call this function when any user will bring mouse over a paragraph written in the example.
<head> <title>Event Handler Example t</title> <script type="text/javascript"> function myAlert() { alert("I am an event handler...."); return; } </script> </head> <body> <span onmouseover="myAlert();"> Bring your mouse here to see an alert </span> </body> |
Now this will produce following result. Bring your mouse over this line and see the result:
Bring your mouse here to see an alert |
To become more comfortable - Do Online Practice
Hide Scripts from Older Browsers
Athough most (if not all) browsers these days support scripts, some older browsers don't. If a browser doesn't support JavaScript, instead of running your script, it would display the code to the user. To prevent this from happening, you can simply place HTML comments around the script. Older browsers will ignore the script, while newer browsers will run it.
JavaScript Example: <script type="text/javascript"> <!-- document.write("Hello Javascript!"); //--> </script> VBScript Example: <script type="text/vbscript"> <!-- document.write("Hello VBScript!") '--> </script> |
The <noscript> Element:
You can also provide alternative info for users whose browsers don't support scripts and for users who have disabled scripts. You do this using the <noscript> tag.
JavaScript Example: <script type="text/javascript"> <!-- document.write("Hello Javascript!"); //--> </script> <noscript>Your browser does not support Javascript!</noscript> VBScript Example: <script type="text/vbscript"> <!-- document.write("Hello VBScript!") '--> </script> <noscript>Your browser does not support VBScript!</noscript> |
Default Scripting Language
You can specify a default scripting language for all your script tags to use. This saves you from having to specify the language everytime you use a script tag within the page. Below is the example:
<meta http-equiv="Content-Script-Type" content="text/JavaScript" /> |
Note that you can still override the default by specifying a language within the script tag.
Advertisements |