HTML Reference
HTML by Alphabet HTML by Function HTML Global Attributes HTML Events HTML Canvas HTML Audio/Video HTML Doctypes HTML Colornames HTML Colorpicker HTML Colormixer HTML Character Sets HTML ASCII HTML ISO-8859-1 HTML Symbols HTML URL Encode HTML Lang Codes HTTP Messages HTTP Methods Keyboard ShortcutsHTML Tags
<!--> <!DOCTYPE> <a> <abbr> <acronym> <address> <applet> <area> <article> <aside> <audio> <b> <base> <basefont> <bdi> <bdo> <big> <blockquote> <body> <br> <button> <canvas> <caption> <center> <cite> <code> <col> <colgroup> <command> <datalist> <dd> <del> <details> <dfn> <dialog> <dir> <div> <dl> <dt> <em> <embed> <fieldset> <figcaption> <figure> <font> <footer> <form> <frame> <frameset> <head> <header> <h1> - <h6> <hr> <html> <i> <iframe> <img> <input> <ins> <kbd> <keygen> <label> <legend> <li> <link> <map> <mark> <menu> <meta> <meter> <nav> <noframes> <noscript> <object> <ol> <optgroup> <option> <output> <p> <param> <pre> <progress> <q> <rp> <rt> <ruby> <s> <samp> <script> <section> <select> <small> <source> <span> <strike> <strong> <style> <sub> <summary> <sup> <table> <tbody> <td> <textarea> <tfoot> <th> <thead> <time> <title> <tr> <track> <tt> <u> <ul> <var> <video> <wbr>HTML <script> Tag
Example
Write "Hello world" with JavaScript:
<script>
document.write("Hello World!")
</script>
document.write("Hello World!")
</script>
Try it yourself »
Browser Support
The <script> tag is supported in all major browsers.
Definition and Usage
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.
Common uses for JavaScript are image manipulation, form validation, and dynamic changes of content.
Tips and Notes
Note: If the "src" attribute is present, the <script> element must be empty.Tip: Also look at the <noscript> element for users that have disabled scripts in their browser, or have a browser that doesn’t support client-side scripting.
Note: There are several ways an external script can be executed:
- If async="async": The script is executed asynchronously with the rest of the page (the script will be executed while the page continues the parsing)
- If async is not present and defer="defer": The script is executed when the page has finished parsing
- If neither async or defer is present: The script is fetched and executed immediately, before the browser continues parsing the page
Differences Between HTML 4.01 and HTML5
The "type" attribute is required in HTML 4, but optional in HTML5.The "async" attribute is new in HTML5.
The HTML 4.01 attribute: "xml:space", is not supported in HTML5.
Differences Between HTML and XHTML
In XHTML, the content inside scripts is declared as #PCDATA (instead of CDATA), which means that entities will be parsed.This means that in XHTML, all special characters should be encoded, or all content should be wrapped inside a CDATA section:
<script type="text/javascript">
//<![CDATA[
var i=10;
if (i<5)
{
// some code
}
//]]>
</script>
//<![CDATA[
var i=10;
if (i<5)
{
// some code
}
//]]>
</script>
Attributes
New : New in HTML5.Attribute | Value | Description |
---|---|---|
asyncNew | async | Specifies that the script is executed asynchronously (only for external scripts) |
charset | charset | Specifies the character encoding used in an external script file |
defer | defer | Specifies that the script is executed when the page has finished parsing (only for external scripts) |
src | URL | Specifies the URL of an external script file |
type | MIME-type | Specifies the MIME type of the script |
xml:space | preserve | Not supported in HTML5. Specifies whether whitespace in code should be preserved |
Global Attributes
The <script> tag also supports the Global Attributes in HTML.Related Pages
HTML tutorial: HTML ScriptsWEB HOSTING
WEB BUILDING
XML Editor - Free Trial! FREE Website BUILDER FREE Website Creator Best Website TemplatesW3Schools is optimized for learning, testing, and training. Examples might be simplified to improve reading and basic understanding.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content.
While using this site, you agree to have read and accepted our terms of use and privacy policy.
Copyright 1999-2013 by Refsnes Data. All Rights Reserved.
No comments:
Post a Comment