1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

onload javascript function not called in firefox 3.6.8

Discussion in 'Web Design & Development' started by truebluexxx, Sep 1, 2010.

Thread Status:
Not open for further replies.
Advertisement
  1. truebluexxx

    truebluexxx Thread Starter

    Joined:
    Aug 6, 2007
    Messages:
    51
    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?
     
  2. truebluexxx

    truebluexxx Thread Starter

    Joined:
    Aug 6, 2007
    Messages:
    51
    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
     
  3. tomdkat

    tomdkat Trusted Advisor

    Joined:
    May 6, 2006
    Messages:
    7,141
    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...
     
  4. truebluexxx

    truebluexxx Thread Starter

    Joined:
    Aug 6, 2007
    Messages:
    51
    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.
     
  5. tomdkat

    tomdkat Trusted Advisor

    Joined:
    May 6, 2006
    Messages:
    7,141
    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...
     
  6. Sponsor

As Seen On
As Seen On...

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.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/947180