Get class item for chrome

I am trying to add this code to the chrome extension to alert me when a chat panel is available. Its in a div with the class name shoutbox so far it doesn't work.

function Detection(){
    if(document.getElementsByClassName("shoutbox")!=null){
      alert('Chat is available.')
    }
}

Detection();

Updated code: the page loads and the warning dialog does not appear.

function Detection(){
    if(document.getElementsByClassName("shoutbox").length > 0){
        alert('Chat is available.')
    }
}

window.onload = Detection;
+5
source share
3 answers

== nullwill not detect an empty array (no results). You can write

if(document.getElementsByClassName("shoutbox").length > 0){
  alert('Chat is available.')
}
+5
source

document.getElementsByClassName("shoutbox")returns an array of elements and returns an empty array if it does not find anything. To find out if elements exist, check the length of the array.

if(document.getElementsByClassName("shoutbox").length > 0){
0
source

chatbox ( - ), DOMSubtreeModified, , - DOM

document.addEventListener('DOMSubtreeModified', function(e) {
    if(document.getElementsByClassName("shoutbox").length > 0){
        alert('Chat is available.')
    }
});
0

All Articles