<script> element can be used without attributes, but some common attributes are used to modify the behavior:
- When provided, the
srcattribute will load an external script.
asyncattribute executes the script as soon as it is loaded, ignoring other pending actions in the browser.
deferattribute requires the script to wait to execute until the content in the
<body>has finished loading into the browser.
<script> element is allowed to be placed both into the
<body>, and their behavior with respect to loading and executing depends on which one they are in, and the attributes they are given.
This example demonstrates the multiple places where a
<script> element may live, and how execution affects their location and attributes:
<html><head><!-- Will load and run file "deferred_app.js" only once the body below is fully loaded into memory --><script defer src="deferred_app.js"></script><!-- Will load and run file "async_app.js" as soon as it finishes loading --><script async src="async_app.js"></script><!-- Will run before the body finishes loading into memory --><script>console.log('Goodbye, World!');</script></head><body><h1>Hello, World!</h1><p>Traditionally, a Hello World application represents the first step a newprogrammer takes into learning a new language.</p><!-- Will naturally run after the other body content is fully loaded into memory --><script>console.log('Hello, World!');</script></body></html>