Live Chat & Podcast at 1:00PM Eastern on Sunday!
There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
Search
Web Design & Development
Tag Cloud
access acer asus bios bsod computer crash desktop driver drivers error ethernet excel freeze gaming hard drive hardware hdmi internet laptop malware memory modem monitor motherboard network printer problem ram registry router security slow software sound toshiba trojan ubuntu 11.10 uninstall usb video virus vista wifi windows windows 7 windows 7 32 bit windows 7 64 bit windows xp wireless
Search
Search for:
Tech Support Guy Forums > Internet & Networking > Web Design & Development >
onload javascript function not called in firefox 3.6.8

Reply  
Thread Tools
truebluexxx's Avatar
Member with 45 posts.
 
Join Date: Aug 2007
Location: Dundee
Experience: Advanced
01-Sep-2010, 02: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
Member with 45 posts.
 
Join Date: Aug 2007
Location: Dundee
Experience: Advanced
01-Sep-2010, 05: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
Distinguished Member with 7,127 posts.
 
Join Date: May 2006
Location: S.F. Bay Area, CA
Experience: Intermediate
01-Sep-2010, 06: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
Member with 45 posts.
 
Join Date: Aug 2007
Location: Dundee
Experience: Advanced
01-Sep-2010, 09: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
Distinguished Member with 7,127 posts.
 
Join Date: May 2006
Location: S.F. Bay Area, CA
Experience: Intermediate
01-Sep-2010, 09: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...
Reply

Tags
javascript

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)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who want to help you solve your computer problems. See our Welcome Guide to get started.
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 12:42 PM
Mozilla Firefox 3.6 Beta MrSooshee Web & Email 2 31-Oct-2009 04:25 PM
Media will not play in firefox. LynxSL Web & Email 13 20-Aug-2008 07:09 PM
Solved: How do organize (do things to) bookmarks in Firefox 3 royeo All Other Software 2 20-Aug-2008 03:45 AM
How to get Firefox 2 add-on extensions working in Firefox 3 lotuseclat79 Linux and Unix 0 25-May-2008 10:33 PM


Facebook Facebook Twitter Twitter TechGuy.tv TechGuy.tv Mobile TSG Mobile
You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 10:05 PM.
Copyright © 1996 - 2011 TechGuy, Inc. All rights reserved.

Powered by Cermak Technologies, Inc.