1

When I'm trying to run the following code in IE:-

<html>
<head>
<script language="javascript">
window.onload=function(){
alert("Window.onload is working");
}
</script>
</head>
<body onload="alert('body.onload is working')">
</body>
</html>

It seems that the body.onload overrides window.onload. I want both of them because I have a page and When I press a button, I get a second file and put it inside a div in the first page. So, I need to use window.onload in the first page and when I get the second one, I use body.onload of the second one.

I don't have this problem in FF,Opera,Chrome and Safari.

I hope my problem is clear. Thanks for any help.

codemaker
  • 177
  • 4
  • 10
  • You might want to 'accept' more answers to the questions you've made, otherwise people will negate answering your questions in the future. – DoctorLouie Jan 22 '10 at 22:12

3 Answers3

1

If you see the following page regarding https://stackoverflow.com/questions/191157/window-onload-vs-body-onload, you'll find that they're the same thing. In other words, you're merely redefining what happens when the load event is fired.

Community
  • 1
  • 1
Dustin
  • 1,858
  • 13
  • 14
1

If you "get [the] second file" using AJAX you should be able to use the response callback to perform any "onload" actions.
If you're using an iframe you should be able to bind to the onload event of the iframe's contentWindow.

brianpeiris
  • 10,424
  • 1
  • 28
  • 44
  • I'm using AJAX and I've tried to use the callback function to bind and event to the body of the second file but I get an error says document.getElementById("") is null. I don't know why! – codemaker Dec 16 '09 at 23:05
  • The information you've given us so far is insufficient to determine the problem. Please show us the code you use to perform and handle the AJAX request as well as sample response data and we can help you from there. – brianpeiris Dec 16 '09 at 23:09
1

Why not put a JavaScript command at the end of the content that's being subsequently loaded into the div? Hence, when the content is loaded the last line is a JavaScript triggering the call to your function. That's pretty much the simple way of making sure that: (1) content is loaded and (2) you create this function with full cross-browser efficiency.

Adrian Mole
  • 30,672
  • 69
  • 32
  • 52
DoctorLouie
  • 2,668
  • 1
  • 16
  • 23