I'm very new to HTML and JS. I have been trying to find a way to redirect a user to a page if they deny camera access but haven't had any luck. Does anybody know how to do it?
Asked
Active
Viewed 285 times
-1
-
2Does this answer your question? [Is there a way to check if geolocation has been DECLINED with Javascript?](https://stackoverflow.com/questions/6092400/is-there-a-way-to-check-if-geolocation-has-been-declined-with-javascript) Instead of 'geolocation', use 'camera'. – Heretic Monkey Jul 15 '21 at 17:57
-
Where's the code you used without luck and what went wrong? https://stackoverflow.com/help/how-to-ask. You should be able to use `navigator.mediaDevices.getUserMedia(constraints).then((stream) => {}).catch((e) =>location.href='');` See https://www.html5rocks.com/en/tutorials/getusermedia/intro/ – Ruan Mendes Jul 15 '21 at 17:57
-
1@JuanMendes Thanks, Perhaps you could read the entire comment. And it's not a different API, since the accepted answer is using the Permissions API, which is the same in both cases. – Heretic Monkey Jul 15 '21 at 18:03
1 Answers
1
Taking an example access from this answer, you could do something like this:
var facingMode = "user"; // Can be 'user' or 'environment' to access back or front camera (NEAT!)
var constraints = {
audio: false,
video: {
facingMode,
}
};
navigator.mediaDevices.getUserMedia(constraints)
.then((stream) => {
// some code
})
.catch((error) => {
// User denied the request
window.location.replace("https://example.com"); // redirect
});