# onload javascript function not called in firefox 3.6.8



## truebluexxx (Aug 6, 2007)

window.document.getElementById('MWD_body').onload = function() 
{
setwidth('page_holder',800);
alert("onload event");
}

This function gets called in IE8 and Google chrome 5.0.375.127 but doesn't work in Firefox 3.6.8.
Can anyone tell me why?


----------



## truebluexxx (Aug 6, 2007)

I changed it to this

window.document.getElementById('MWD_body').onload = setwidth('page_holder',800);

the function now gets called in IE8, firefox and Chrome, but it creates a 'not implemented' error with IE 8, 
If I change the function to take zero parameters then change the code to 
window.document.getElementById('MWD_body').onload = setwidth; (notice the () are missing)
This removes the IE8 'not implemented error but doesn't call the function in firefox. my head is sore


----------



## tomdkat (May 6, 2006)

Here is a test case that works in Firefox 3.6.8:


```
<html>
<head>
<title>JavaScript test</title>
<style type="text/css">
#targetElement {
border: 2px solid red;
width: 500px;
height: 200px;
margin: 0 auto 0 auto;
}
</style>
<script type="text/javascript">
function setWidth(element,width)
{
	alert("Setting width of element '"+element+"' to: "+width);
}
</script>
</head>
<body onload="setWidth('targetElement',800);">
<p>This is a paragraph</p>
<div id="targetElement">
<p>This is the target text!</p>
</div>
</body>
</html>
```
In your HTML, is 'MWD_body' a different element or something? Can you post a test case?

Peace...


----------



## truebluexxx (Aug 6, 2007)

thanks for the reply MWD_body is the id of the body tag
I want to have my javascript separate from the html so 
is not what I want, but I fixed the problem anyway.
I shouldn't have the brackets after the function name as explained here
http://www.sitepoint.com/forums/showthread.php?t=638968

and replacing window.document.getElementById('MWD_body').onload =setwidth;
with window.onload = setwidth;
works. but I don't know why there is a difference between the two.


----------



## tomdkat (May 6, 2006)

truebluexxx said:


> and replacing window.document.getElementById('MWD_body').onload =setwidth;
> with window.onload = setwidth;
> works. but I don't know why there is a difference between the two.


According to the Mozilla Developer Center (MDC), the "onload" event handler for the load event of the window object. So, you don't need to fetch the "body" element, as you're doing, since the "onload" event will fire when the page has finished loading all of its elements. Also, according to the MDC, "window.onload" isn't part of any standard so I don't know if that's guaranteed to work across browsers today and in the future.

Glad you got it figured out. It makes sense that having the parentheses after the function name would cause the function *itself* to be called instead of assigning the function to the event handler. 

Peace...


----------

