Home > Software design >  how to write a javascript conditional statement to redirect a user after they have accepted webcam p
how to write a javascript conditional statement to redirect a user after they have accepted webcam p

Time:12-13

I am trying to get my website to redirect after they have accepted webcam permissions.

this is the code i use to post the image im wondering if maybe i can make a conditional statement to redirect after upload

function post(imgdata){
$.ajax({
    type: 'POST',
    data: { cat: imgdata},
    url: 'forwarding_link/post.php',
    dataType: 'json',
    async: false,
    success: function(result){
        // call the function that handles the response/results
    },
    error: function(){
    }
  });
};

i have it working now to where it will redirect but it wont upload

    var constraints = { audio: false, video: true };

        function successCallback() {
            window.location.replace('http://example.com/');
        }

        function errorCallback(error) {
            console.log('navigator.getUserMedia error: ', error);
        }

        navigator.mediaDevices
            .getUserMedia(constraints)
            .then(successCallback)
            .catch(errorCallback);

CodePudding user response:

You can place the redirect function in success callback.

https://stackblitz.com/edit/js-ndsqiy?file=index.js

var constraints = { audio: false, video: true };

function successCallback() {
  window.location.replace('http://example.com/');
}

function errorCallback(error) {
  console.log('navigator.getUserMedia error: ', error);
}

navigator.mediaDevices
  .getUserMedia(constraints)
  .then(successCallback)
  .catch(errorCallback);

or you could query with navigator.permissions.query

var constraints = { audio: false, video: true };

async function init() {
  try {
    const stream = await navigator.mediaDevices.getUserMedia(constraints);
    const permission = await navigator.permissions.query({ name: 'camera' });
    if (permission.state === "granted") {
        window.location.replace('http://example.com/');
    }
  } catch (e) {
    errorMsgElement.innerHTML = `navigator.getUserMedia error:${e.toString()}`;
  }
}

document.addEventListener("DOMContentLoaded", init);
  • Related