0

i'm doing a script to know if my browser supports the screen sharing. My Script works on every browser (Chrome, Opera, Firefox, Edge, Safari) except Internet Explorer and i don't know what is the problem.

Can you give me the answer please

var getDisplayMedia;
    // Screen sharing is supported by the browser 
    if (navigator.mediaDevices.getDisplayMedia || navigator.getDisplayMedia){
        getDisplayMedia = "O";
    }else {
        getDisplayMedia = "N";
    }
    console.log(getDisplayMedia);

On Chrome, Opera, Firefox, Edge, Safari it returns O or N. but on IE got this error : script5007 unable to get property 'getDisplayMedia' of undefined or null reference

Be Weria
  • 25
  • 8

2 Answers2

1

According to the browser compatibility section of the MediaDevices documentation, it has no support in IE.

I suggest checking the navigator.userAgent for the indexOf("MSIE") before checking the mediaDevices properties.

Ezra
  • 1,118
  • 9
  • 13
  • Yes so then it should put getDisplayMedia to "N". but there is script5007 unable to get property 'getDisplayMedia' of undefined or null reference. So my script doesnt works – Be Weria May 22 '19 at 10:42
  • @BeWeria I updated the answer with a potential workaround. – Ezra May 22 '19 at 10:43
  • There is my userAgent on IE : "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; .NET4.0C; .NET4.0E; rv:11.0) like Gecko". indexof("MSIE") will return false – Be Weria May 22 '19 at 10:45
  • @BeWeria For IE 11, you'll need to use a regex to match `/Trident.*rv\:11\./`. I'm sorry it's a bit verbose, but I'm not by my computer to try anything else out. Props to [this answer](https://stackoverflow.com/a/19999868/8108612) for the matcher – Ezra May 22 '19 at 10:49
  • @BeWeria Yes, always implement the simpler solution! – Ezra May 22 '19 at 10:51
1

You need to check that navigator.mediaDevices exists before checking one of its properties.

var getDisplayMedia;
// Screen sharing is supported by the browser 
if (navigator.mediaDevices && navigator.mediaDevices.getDisplayMedia || 
    navigator.getDisplayMedia
){
    getDisplayMedia = "O";
}else {
    getDisplayMedia = "N";
}

console.log(getDisplayMedia);
Simon Brahan
  • 2,016
  • 1
  • 14
  • 22