首页 > 解决方案 > 如果用户拒绝摄像头访问,如何将用户重定向到单独的页面?

问题描述

我对 HTML 和 JS很陌生。如果用户拒绝相机访问但没有任何运气,我一直在尝试找到一种将用户重定向到页面的方法。有人知道怎么做吗?

标签: javascripthtml

解决方案


以this answer为例,您可以执行以下操作:

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
  });

推荐阅读