How to Detect DOM and API Resources

Asked

Viewed 106 times

4

How to know if the browser supports certain methods, estates 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.');

}

1 answer

8


Use the operator in to check if a certain property exists in the object, for example:

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

if('classList' in document.createElement('div')){
  alert("Suporta 'classList'.");  
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.