最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

will HTML <body> onLoad events overwrite javascript window onload event? - Stack Overflow

programmeradmin0浏览0评论

I have a HTML page and a javascript function is attached to the <body> onLoad event.

I wanted to show up a message dialog when the page loads. I cannot edit the javascript function attached to this onLoad event due to some reasons.

So I created a new javascript file with a single function which will show the message dialog. Then I added this line in my javascript file

window.onload = onPageLoad; 

onPageLoad() is my function which could show the message dialog.

I attached this javascript file in my HTML using script tag. When I run this HTML file, onPageLoad() function is not getting called.

I want to know whether <body> tag, onLoad event overrides the window onload. If so, can someone help me in implementing this functionality somehow.

Please keep in mind that I could not edit my HTML file and I could write only new javascript file. Thanks.

I have a HTML page and a javascript function is attached to the <body> onLoad event.

I wanted to show up a message dialog when the page loads. I cannot edit the javascript function attached to this onLoad event due to some reasons.

So I created a new javascript file with a single function which will show the message dialog. Then I added this line in my javascript file

window.onload = onPageLoad; 

onPageLoad() is my function which could show the message dialog.

I attached this javascript file in my HTML using script tag. When I run this HTML file, onPageLoad() function is not getting called.

I want to know whether <body> tag, onLoad event overrides the window onload. If so, can someone help me in implementing this functionality somehow.

Please keep in mind that I could not edit my HTML file and I could write only new javascript file. Thanks.

Share Improve this question edited Mar 30, 2019 at 13:28 mplungjan 178k28 gold badges181 silver badges240 bronze badges asked Mar 4, 2011 at 13:59 user131476user131476 5221 gold badge7 silver badges20 bronze badges 2
  • can you attach some codes? try use domready instead – KJYe.Name Commented Mar 4, 2011 at 14:07
  • Can you add the code to the body?, i.e. at the bottom of the page? Also what is the code you are calling in the body onload. You may be able to replace the function – mplungjan Commented Mar 4, 2011 at 14:18
Add a ment  | 

1 Answer 1

Reset to default 6

Depends on browser. window.onload currently overwrites body onload in Chrome, Firefox and Safari on OSX

You can ADD your function to the onload:

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>

AND/OR Replace

var bodyOnload = document.body.onload;
window.onload = function() {
  alert('window.onload')
  bodyOnload()
}

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>

发布评论

评论列表(0)

  1. 暂无评论