Will the HTML <body> onLoad events overwrite the onload javascript window event?

I have an HTML page and the javascript function is attached to the <body>onLoad event . I wanted to show a message dialog when the page loads. I cannot edit the javascript function attached to this onLoad event for some reason. So I created a new javascript file with a single function that displays a message dialog. Then I added this line to my javascript file "window.onload = onPageLoad;"(onPageLoad () is my function that can display a message dialog). I attached this javascript file in my HTML using the script tag. When I run this HTML file, the onPageLoad()function is not called I want to know what tag<body>, the onLoad event overrides the onload window. If so, can someone help me in implementing this function somehow. Keep in mind that I could not edit my HTML file and I could only write a new javascript file. Thank.

+3
source share
1 answer

Depends on the browser. window.onload is currently overwriting body loading in Chrome, Firefox and Safari on OSX

You can add a function to the download:

window.onload = function() {
  alert('window.onload')
}

if (window.addEventListener) {
  window.addEventListener('load', function() {
    alert('addEventListener')
  }, false);
} else if (window.attachEvent) { // IE < 9
  window.attachEvent('onload', function() {
    alert('attachEvent')
  });
}
<body onload="alert('body onload')">

</body>
Run code
+1
source

Source: https://habr.com/ru/post/1796304/


All Articles