I am trying to detect the presence of an element on the page (#navDropDowns) in Javascript to determine if a user is logged in. The element is not present on load but is later added by Salesforce's own JS events. My script is also loaded into the page via Salesforce's JS routines.
I've been trying to use setInterval and keep checking the page. Surprisingly, it always fails (reporting 'logged out'. However, when I run it direct in the browser console, it reports 'logged in'.
(function wpSalesforce() {
setInterval( function(){
if ( document.getElementById('navDropDowns') !== null ) {
console.log('logged in')
document.body.className += ' ' + 'logged-in';
} else {
console.log('logged out')
}
}, 1000 );
})();
I've tried various versions of this including variable assignment but am getting nowhere. As it works in the browser console, it is as if setInterval is using an old version of the DOM.
I'm unable to use jQuery for this and it needs to be pure JS.