DOMContentLoaded sometimes work and sometimes doesn't while setTimeout usually always work.
For example, the following code works:
setTimeout( ()=>{
let sites = ['mako.co.il'];
let regex = /\..+/;
let href = window.location.href;
for (let i = 0; i < sites.length; i++) {
if (href.includes(sites[i])) {
let domain = sites[i].replace(regex, '');
document.body.innerHTML =`
<div style="direction: ltr; position: fixed; top: 0; z-index: 999999; display: block; width: 100%; height: 100%; background: red">
<p style="position: relative; top: 40%; display: block; font-size: 66px; font-weight: bold; color: #fff; margin: 0 auto; text-align: center">
Enough with this ${domain} bullshit!
</p>
</div>
`;
}
}
}, 1500);
But if instead setTimeout( ()=>{...}, 1500);
I would use document.addEventListener('DOMContentLoaded', ()=>{...});
it won't.
Why is that?
In both cases I wait a certain time period and then execute the code. What could be the case that after all DOM tree loaded, the code fails?