1

Both firefox and safari refuses to execute load event when loaded from an external js file. It only works on google chrome.

I can't really understand the problem.

in my html :

<script src="/assets/js/pages/myFile.js" type="text/javascript"></script>

in myFile.js :

window.addEventListener("load", function(event){
// do someting (only works in chrome browser)
})

My html page is serverd by node.js (ejs page), myFile.js is recognized by the 3 browsers (Firefox, chrome & safari), so I don't really understand why my load event fail with ff & safari.

I've also tried window.unload unsuccessfully.

Any suggestion ?

John doe
  • 3,680
  • 7
  • 31
  • 65
  • I think we'd have to see a reproducible case in order to know how to help. Clearly the window `load` event works in general so it apparently has something to do with how you're doing things. Please add a minimal, reproducible example to your question so we can see the problem occurring and reproduce it and test things out. – jfriend00 Dec 25 '21 at 01:07
  • For a related answer, see [How to call a function when DOM is ready](https://stackoverflow.com/questions/9899372/pure-javascript-equivalent-of-jquerys-ready-how-to-call-a-function-when-t/9899701#9899701). – jfriend00 Dec 25 '21 at 01:08

1 Answers1

0

Seems like event is already fired, I'm suggest to always check for it:

function onLoad(callback){
    if (document.readyState === 'complete') {
        callback();
    } else {
        window.addEventListener("load", callback);
    }
}

onLoad(function(){
    // do someting
});
7luc9ka
  • 28
  • 4