Sub topic 17
Cross-origin media and CORS
This chapter assumes you can write basic HTML, read JavaScript with promises, and know what an origin is (scheme, host, and port together). It builds on the source and track elements from earlier chapters.
Playing is allowed, reading is not
A page on https://app.example can play a video hosted on https://media.example. The browser fetches the file and plays it without any special headers. This is a "no-CORS" request, and the media is treated as opaque. The page can show and hear it, but it can't read the pixels or samples.
CORS (Cross-Origin Resource Sharing) is the mechanism that lifts that restriction. The media server opts in by sending an Access-Control-Allow-Origin header. The page opts in by adding the crossorigin attribute.
When you need crossorigin
Add crossorigin when your code must read the media data or when the browser demands CORS for a related file. Common cases:
- Drawing a video frame to a
<canvas>and then reading it back withgetImageData()ortoDataURL(). - Feeding an element into the Web Audio API with
createMediaElementSource(). - Loading a
<track>file from another origin. Text tracks always require CORS, so a captions file on a different origin fails without it.
<video controls crossorigin="anonymous" width="640">
<source src="https://media.example/clip.mp4" type="video/mp4">
<track kind="captions" src="https://media.example/clip.en.vtt"
srclang="en" label="English" default>
</video>The value anonymous sends the request without cookies. Use use-credentials only if the server needs cookies, and then the server must also send Access-Control-Allow-Credentials: true and name your exact origin instead of *.
The server side
The server hosting the media must answer with a matching header:
Access-Control-Allow-Origin: https://app.exampleYou can use * for public, credential-free media. If the header is missing or doesn't match, the browser blocks the request. Because crossorigin turns the media request into a CORS request, the video won't play at all, not just fail to be readable.
The sequence is easier to follow as a diagram.
Common errors
- Canvas is "tainted." Calling
toDataURL()throws aSecurityError. The video was loaded withoutcrossorigin, or the server sent no CORS header. - Video stops working after adding
crossorigin. The server doesn't send the CORS header. Fix the server, or remove the attribute if you only need playback. - Captions don't appear. The
.vttfile is on another origin without CORS headers. Check the browser's network panel. - Header set only on some responses. Range requests (partial content) and error responses must include the header too.
Exercise
A video from another origin plays fine, but your code throws a SecurityError when it calls canvas.toDataURL() after drawing a frame. What two changes fix this?
Show answer
Add crossorigin="anonymous" to the video element, and make the media server send an Access-Control-Allow-Origin header that allows your page's origin (or *). Without both, the canvas stays tainted.