Example 1 — Detect + Secure Context
Confirm HTTPS / localhost together with navigator.mediaDevices.
const lines = [
"secure: " + window.isSecureContext,
"mediaDevices: " + Boolean(navigator.mediaDevices)
];
if (!window.isSecureContext) {
lines.push("Serve over HTTPS (or localhost) to use mediaDevices");
} else if (!navigator.mediaDevices) {
lines.push("Secure, but MediaDevices API not exposed");
} else {
lines.push("Ready for getUserMedia / enumerateDevices");
}
console.log(lines.join("\n")); How It Works
- Check
isSecureContext. MDN requires a secure context. - Check the property. Missing on insecure or very old engines.
- Print a clear hint. Helps debug http:// vs https://.
- Only then call methods. Avoids confusing “undefined” errors.

