tags:

views:

324

answers:

3

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.

+1  A: 

If you see the following page regarding http://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.

Dustin
+1  A: 

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
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
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
A: 

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 a call to your function. That's pretty much the simple way of making sure #1 content is loaded and #2 you create this function with full cross-browser efficiency.

drlouie - louierd
Thanks, I'm going to try it
codemaker
Cool flag me up if my idea works out fine, ite should be perfect :)
drlouie - louierd
It was a good idea. Thanks it works
codemaker
Give me an uptick on this answer buddy, since it worked!
drlouie - louierd