How to Detect DOM and API Resources

4

How to tell if your browser supports certain methods , properties and events

Checking whether an object querySelector , querySelectorAll , addEventListener , classList exists via Javascript.

I've been trying something with if .. else a flow control structure based on a condition.

Example

// Criando o elemento para verificar 
var elem = document.createElement('classList');

if(elem != "undefined"){

alert ('Existe sim.');

} else {

alert ('Não existe.');

}
    
asked by anonymous 02.07.2016 / 05:46

1 answer

8

Use the in a> to check if a given property exists in the object, for example:

if('querySelector' in document){
  alert("Suporta 'querySelector()'.");
}

if('classList' in document.createElement('div')){
  alert("Suporta 'classList'.");  
}
    
02.07.2016 / 07:52