HTML Scripts
A script is a small, embedded 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.
Because HTML doesn't actually have scripting capability, you need to use the <*script> tag to generate a script, using a scripting language.
The <*script> tags tell the browser to expect a script in between them. You specify the language using the type attribute. The most popular scripting language on the web is JavaScript.
The <*script> tag is used to define a client-side script, such as a JavaScript.
The script element either contains scripting statements or it points to an external script file through the src attribute.
The required type attribute specifies the MIME type of the script.
Common uses for JavaScript are image manipulation, form validation, and dynamic changes of content.
The script below writes Hello World! to the HTML Code:
Results:
TRY IT YOURSELF |
Triggering a Script
In many cases, you won't want the script to run automatically. You might only want the script to run if the user does something (like hover over a link), or once the page has finished loading.
These actions are called intrinsic events (events for short). There are 18 pre-defined intrinsic events that can trigger a script to run. You use event handlers to tell the browser which event should trigger which script. These are specified as an attribute within the HTML tag.
Lets say you want a message to display in the status bar whenever the user hovers over a link. The act of hovering over the link is an event which is handled by the onmouseover event handler. You add the onmouseover attribute to the HTML tag to tell the browser what to do next.
HTML Code:
Results:
TRY IT YOURSELF |
Calling an External Script
You can also place your scripts into their own file, then call that file from your HTML document. This is useful if you want the same scripts available to multiple HTML documents - it saves you from having to "copy and paste" the scripts into each HTML document. This makes it much easier to maintain your website.
HTML Code:
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.
HTML Code:
Alternate Information for Older Browsers
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.
HTML Code:
Set a 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.
HTML Code:
HTML Scripts
« Previous Next Chapter »
JavaScripts make HTML pages more dynamic and interactive.
Examples
Try-It-Yourself Examples
Insert a script
How to insert a script into an HTML document.
Use of the <*noscript> tag
How to handle browsers that do not support scripting, or have scripting disabled.
The HTML script Element
The <*script> tag is used to define a client-side script, such as a JavaScript.
The script element either contains scripting statements or it points to an external script file through the src attribute.
The required type attribute specifies the MIME type of the script.
Common uses for JavaScript are image manipulation, form validation, and dynamic changes of content.
The script below writes Hello World! to the HTML output:
Example
Try it yourself »
Remark Tip: To learn more about JavaScript, visit our JavaScript tutorial!
The HTML noscript Element
The <*noscript> tag is used to provide an alternate content for users that have disabled scripts in their browser or have a browser that doesn’t support client-side scripting.
The noscript element can contain all the elements that you can find inside the body element of a normal HTML page.
The content inside the noscript element will only be displayed if scripts are not supported, or are disabled in the user’s browser:
Example
<*script type="text/javascript">
document.write("Hello World!")
<*/script>
<*noscript>Sorry, your browser does not support JavaScript!<*/noscript>
Try it yourself »
HTML Script Tags
Tag Description
<*script> Defines a client-side script
<*noscript> Defines an alternate content for users that do not support client-side
scripts