CKTIS / HTML
crossorigin: choose a CORS request mode
Coordinate the browser request with permission from the resource server.
Version: HTML Living Standard
Syntax
<img crossorigin="anonymous" src="https://images.knowledge.test/pixel.svg" alt="Red pixel">On supported image, media, script and link elements, crossorigin controls CORS-related fetching. anonymous uses CORS with same-origin credentials; use-credentials includes credentials. An empty or invalid value means anonymous. Omitting the attribute is different: some resources use no-CORS, while modules always require CORS and use the setting for credentials.
The server must return suitable CORS headers; the attribute cannot grant access by itself. Credentialed responses require an explicit allowed origin and permission for credentials, not a wildcard origin. Set the mode before starting the request. Displaying a cross-origin image does not automatically allow reading its pixels from canvas.
Two isolated origins serve the same image with anonymous CORS permission. Pixel reading succeeds for the CORS image but throws a security error for the ordinary cross-origin image. No real credentials or external server are involved.
Examples
<!doctype html><meta charset="utf-8"><output id="result"></output><script>
async function readPixel(cors) {
const image = new Image();
if (cors) image.crossOrigin = 'anonymous'; // Set before starting the request.
image.src = 'https://images.knowledge.test/pixel.svg?' + (cors ? 'cors' : 'plain');
await image.decode();
const canvas = document.createElement('canvas'); canvas.width = canvas.height = 1;
const context = canvas.getContext('2d'); context.drawImage(image, 0, 0);
try { return context.getImageData(0, 0, 1, 1).data[0]; }
catch (error) { return error.name; }
}
// The intercepted second-origin image response explicitly allows anonymous CORS.
Promise.all([readPixel(true), readPixel(false)]).then(([cors, plain]) => {
document.querySelector('#result').textContent = 'cors=' + cors + '; plain=' + plain;
});
</script>Result
cors=255; plain=SecurityError
Both origins are intercepted locally; the response headers and the canvas security outcome are verified.