Advertisement

There's no such thing as a stupid question, but they're the easiest to answer.
Login
Search

Advertisement

Web Design & Development Web Design & Development
Search Search
Search for:
Tech Support Guy > > >

onload javascript function not called in firefox 3.6.8


(!)

truebluexxx's Avatar
truebluexxx truebluexxx is offline truebluexxx has a Profile Picture
Member with 51 posts.
THREAD STARTER
 
Join Date: Aug 2007
Location: Dundee
Experience: Advanced
01-Sep-2010, 12:45 PM #1
onload javascript function not called in firefox 3.6.8
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's Avatar
truebluexxx truebluexxx is offline truebluexxx has a Profile Picture
Member with 51 posts.
THREAD STARTER
 
Join Date: Aug 2007
Location: Dundee
Experience: Advanced
01-Sep-2010, 03:49 PM #2
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's Avatar
Computer Specs
Trusted Advisor with 7,141 posts.
 
Join Date: May 2006
Location: S.F. Bay Area, CA
Experience: Intermediate
01-Sep-2010, 04:15 PM #3
Here is a test case that works in Firefox 3.6.8:

Code:
<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's Avatar
truebluexxx truebluexxx is offline truebluexxx has a Profile Picture
Member with 51 posts.
THREAD STARTER
 
Join Date: Aug 2007
Location: Dundee
Experience: Advanced
01-Sep-2010, 07:48 PM #4
thanks for the reply MWD_body is the id of the body tag
I want to have my javascript separate from the html so
<body onload="setWidth('targetElement',800);"> 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's Avatar
Computer Specs
Trusted Advisor with 7,141 posts.
 
Join Date: May 2006
Location: S.F. Bay Area, CA
Experience: Intermediate
01-Sep-2010, 07:56 PM #5
Quote:
Originally Posted by truebluexxx View Post
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...
As Seen On

BBC, Reader's Digest, PC Magazine, Today Show, Money Magazine
WELCOME TO TECH SUPPORT GUY!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.


Tags
javascript

(clock)
THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Search Tech Support Guy

Find the solution to your
computer problem!




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


Similar Threads
Title Thread Starter Forum Replies Last Post
Problem with Outlook Web Access in Firefox 3.6 pachamama67 Business Applications 1 17-Feb-2010 11:42 AM
Mozilla Firefox 3.6 Beta MrSooshee Web & Email 2 31-Oct-2009 02:25 PM
Media will not play in firefox. LynxSL Web & Email 13 20-Aug-2008 05:09 PM
Solved: How do organize (do things to) bookmarks in Firefox 3 royeo All Other Software 2 20-Aug-2008 01:45 AM
How to get Firefox 2 add-on extensions working in Firefox 3 lotuseclat79 Linux and Unix 0 25-May-2008 08:33 PM

WELCOME
You Are Using: Server ID
Trusted Website Back to the Top ↑