Inline JavaScript is script code written directly in an HTML page instead of loaded from a separate .js file. It appears in two forms: a <script> element containing code, and event handler attributes such as onclick="..." on an element.
Inline scripts are quick for demos and tiny pages, but external files are easier to cache, test and secure. Understanding both forms also helps you read older code and configure a Content Security Policy correctly.
Put JavaScript between <script> and </script>. The browser runs it as soon as the parser reaches it, in order with the rest of the document.
Place the script after the elements it uses, or at the end of <body>, because the element must already exist when the code runs. Scripts in <head> run before <body> is parsed unless you handle DOMContentLoaded.
An event handler attribute holds a JavaScript statement that runs when the event fires.
Inside the attribute, this is the element and event is the event object. This style is short but mixes behaviour into markup, and it only supports one handler per attribute.
Attach the listener from a script instead. It keeps HTML clean, allows several listeners per event, and works under a strict security policy.
An external script moves the code into a file that the browser can cache across pages.
| Attribute | Download | Runs |
|---|---|---|
| none | Blocks parsing | Immediately, in order |
defer | In parallel | After parsing, in order, before DOMContentLoaded |
async | In parallel | As soon as downloaded, in any order |
type="module" | In parallel | Deferred by default, with import and export support |
Use defer for scripts that need the DOM, and type="module" for modern code split into modules. Use async only for independent scripts such as analytics.
Pages often need to hand data to JavaScript. Putting user content in a normal inline script is a common source of cross-site scripting (XSS) bugs. A safer pattern is a JSON data block, which the browser does not execute.
When you generate that JSON on a server, escape the characters <, > and & (for example as <) so that user text can never contain a closing </script> sequence.
A strict Content Security Policy (CSP) is the main reason to avoid inline code. A header such as Content-Security-Policy: script-src 'self' blocks inline <script> blocks, inline event handler attributes, and javascript: URLs, which stops injected scripts from running.
If you must keep an inline script, allow it explicitly with a per-response nonce.
Generate a new random nonce for every response. A fixed nonce offers no protection. Avoid the 'unsafe-inline' keyword, which turns the protection off.
Two older inline techniques are best avoided.
A javascript: URL breaks without scripting, is not a real link for assistive technology, and is blocked by a strict CSP. document.write() is also discouraged: it can block parsing and is ignored or harmful in many situations, so build content with DOM methods instead.
<html>.For everything else, prefer external modules loaded with defer.
It is JavaScript placed directly in the HTML file, either inside a <script> element or inside an event handler attribute such as onclick.
Search engines run inline and external scripts alike, so it is not an SEO penalty by itself. External files help performance through caching, which indirectly helps page speed.
The script ran before the element was parsed. Move the script after the element, add defer to an external file, or wait for DOMContentLoaded.
Add a per-response random nonce to the policy and to each allowed <script nonce="..."> tag. Do not rely on 'unsafe-inline'.
Inline JavaScript is code written in a <script> block or an event handler attribute. It is convenient for tiny pages, but external, deferred modules with addEventListener are easier to maintain and work with a strict Content Security Policy. When you must inline code, keep it small, escape any server data, and protect it with a nonce.