HTML and CSS Reference
script in the document and link to an external file. If you do, most browsers will only run the external script
and will ignore the embedded contents of the element. Also note that even when linking to an external
script, the script element still requires a closing </script> tag; it's not a void element even when it's
empty, so it always needs an end tag.
The script element often appears in a document's head element, but it can also appear within the
document's body . It's not uncommon to find scripts embedded or attached at the very end of a document
so they load and execute last, after the rest of the page has been parsed. Scripts that must execute before
the page fully loads—especially those that generate HTML for parts of the page itself—should still appear
in the header so browsers can execute them at the correct time.
There's no specified limit to the number of script elements that can appear within a single document,
either in the head or the body . However, browsers may have their own internal limitations if you try to link
to a huge number of external scripts, so it's best to keep the number of script elements to a sensible
minimum. There can also be serious performance issues when scripts become too numerous or complex,
and it's possible that commands and functions written into those separate scripts can conflict with each
other if you're not careful.
There are no required attributes for the script element.
async : a Boolean attribute that, when present, suggests that browsers should execute the script
asynchronously, as soon as it is available. Only valid in combination with a src attribute
(embedded scripts are always processed as the browser reads them). The async attribute is new
in HTML5 so older browsers—and some current browsers—don't support it.
charset : defines the character encoding of an external script. This is only valid in combination
with a src attribute (embedded scripts automatically inherit the character encoding of the HTML
defer : a Boolean attribute that, when present, suggests that browsers should defer execution of
the external script until the page has finished loading. This is only valid in combination with a src
attribute (embedded scripts are processed as the browser reads them).
src : specifies the URL (either relative or absolute) of an external script file.
type : indicates the content type of the external or embedded script, almost always
Past versions of HTML included a language attribute for the script element, to
indicate the scripting language within and even the specific version of that language.
The language attribute has long been deprecated in favor of the type attribute and it's
completely obsolete in HTML5.