The script
element can include scripting in your pages, either defined inline in the document or referenced to an external file.
The most commonly used type of script is JavaScript.
You use one script element for each script.
The script element has local attributes: type
, src
, defer
, async
, charset
.
A start and end tag for script element are required; self-closing tags are not permitted, even when referencing an external JavaScript library
The type
attribute is optional in HTML5. The async
and defer
attributes have been added in HTML5.
src
attribute.An inline script is included the JavaScript statements in the HTML page.
<!DOCTYPE HTML> <html> <head> <script> document.write("This is from the script"); </script> </head> <body> <a href="http://www.studentsempire.com">click </body> </html>
By default, scripts are executed as soon as they are encountered in the page. You usually use the script element inside the head element, but you may use it anywhere in an HTML document.
You can separate scripts into separate files and load them using the script element.
The file contains a single statement. Then we can use the src attribute in the script element to reference this file.
A script element must be empty if it uses the src attribute. You can't use the same script element to define an inline script and an external script.
An end tag for the script element is needed, even though the element has no content. If you use a self-closing tag when referencing an external script, the browsers will ignore the element and not load the file.
You can control the execution of a script by using the async and defer attributes.
The defer attribute tells the browser not to execute the script until the page has been loaded and parsed.