body onload not executed

Discussion in 'Javascript' started by Tom de Neef, Feb 14, 2012.

  1. Tom de Neef

    Tom de Neef Guest

    I help someone with a XHTML site. It loads dozens of JavaScript files and a
    lot of processing is going on during the loading of a page.
    The script I am interested in is called via <body onload="getChartInfo()">.
    The dubugger (in IE, FF or Chrome) tells me that the necessary js file has
    been loaded. But a breakpoint at the start of the function body is never
    reached.
    In a test environmet - where I have removed all the other scripts - things
    work fine.
    There is only one <body> tag and the page is presented in the browser. I
    would say that the onload event must have occurred. What is a likely reason
    that the event handler is not called?
    TIA
    Tom
     
    Tom de Neef, Feb 14, 2012
    #1
    1. Advertisements

  2. Tom de Neef

    Evertjan. Guest

    Tom de Neef wrote on 15 feb 2012 in comp.lang.javascript:
    When JS has encountered an error,
    preventing correct parsing of getChartInfo() ?


    Try:

    <script type='text/javascript'>

    blah(; // missing ")"

    // much more code

    function a(){
    alert('ok');
    };

    </script>

    <body onload='a();'>
     
    Evertjan., Feb 14, 2012
    #2
    1. Advertisements

  3. Tom de Neef

    Tom de Neef Guest

    When I introduce an error like that IE debugger will report a syntax error
    when loading the page.
    In my case no errors are reported.

    I have now checked the attributes and properties of the body element: onload
    is empty.
    That is the status when the page has been loaded. I can not check the status
    during the processing of all the scripts.
    The html surely contains <body onload="getChartInfo()">.
    Can it be that one of the scripts 'removes' the body.onload assignment and
    if so what would I have to do to find out where it happens?
    Tom
     
    Tom de Neef, Feb 15, 2012
    #3
  4. Search through all the js files that the site calls in to see if anything
    is changing the onload event handler of the body element.

    You may have to spend some time identifying and grabbing all the script
    files.

    If you find that multiple scripts change (or try to change) the onload
    handling, you'll need to then identify which one is the culprit.

    Rgds

    Denis McMahon
     
    Denis McMahon, Feb 15, 2012
    #4
  5. Tom de Neef

    Tom de Neef Guest

    Hmm. I try to do someone a favour ;-)
    Anyway, if that's what needs to be done, I'll do it.
    I was hoping there would be a way to test if body.onload is assigned at
    regular points, say after a script has been loaded. But without modifying
    the html source.
    Tom
     
    Tom de Neef, Feb 15, 2012
    #5
  6. Tom de Neef

    Evertjan. Guest

    Tom de Neef wrote on 15 feb 2012 in comp.lang.javascript:
    Why didn't you tell about which browser and what errors?

    On my Chrome my example acts like you posted.
    What does "empty" mean? Returning "null"?

    Chrome & IE9:

    =====================
    <body>

    <script type='text/javascript'>
    alert(body.onload); // null
    </script>
    =====================

    =====================
    <body onload='alert(1)'>

    <script type='text/javascript'>
    alert(body.onload); // function onload(**) {alert(1)}
    </script>
    =====================
    ** Chrome: event

    Simple debugging with remarking /*....*/ of parts of the script.

    Simple ancient debugging gets you to the error more reliable,
    be it slowly, than this modern js-console thing, as error generation is
    also done by software that might in itself be affected by the error..
     
    Evertjan., Feb 15, 2012
    #6
  7. Hmm, could try downloading the page in ff, then running a grep over any js
    in the "files" directory.

    Or post the url of the web page concerned and see if anyone is bored
    enough to look at it. ;)

    Rgds

    Denis McMahon
     
    Denis McMahon, Feb 15, 2012
    #7
  8. Assuming that some other script fiddles with the onload handler
    of the body element, a very simple workaround would be to put
    the call into a script element just before the closing body tag,
    thus:

    ....
    <script>getChartInfo();</script>
    </body>
    </html>

    Hans-Georg
     
    Hans-Georg Michna, Feb 15, 2012
    #8
  9. In comp.lang.javascript message <4f3ae9c5$0$6918$4
    Try <body onload="alert(12345);getChartInfo()">.

    And scan all files, HTML and JS, for any other occurrences of onload
    which might have overridden that.

    If you have no more than treble-checked the spelling of getChartInfo,
    try a few more times.

    Scan the files for any possible assignment to getChartInfo.

    Replace getChartInfo() by qwerty() in body onload, and provide
    function qwerty() { alert(98765) } to see if that works.

    Also : I'd starting testing a function with a button that executed the
    function provided in a <script> element. It seemed OK, so I tidied the
    code itself and changed the element to <script type-"text/javasript">.
    It ceased to work, with nothing appearing in the Error Console.

    Hint - although your English seems perfect, your keying is not always
    quite so.
     
    Dr J R Stockton, Feb 15, 2012
    #9
  10. Try commenting out code to isolate what is giving the trouble.

    I just had a problem today with code that broke when it should
    not have. Divide and conquer. Somehow, a segment of code got munged.
    It is supposed to be a duplicate of another section so I just copied
    from the other section, and bang: page working again.

    It is not true that the language is out to get you, because TODO:
    Come up with reason.

    [snip]

    Sincerely,

    Gene Wirchenko
     
    Gene Wirchenko, Feb 16, 2012
    #10
  11. Tom de Neef

    Tom de Neef Guest

    I have followed this approach and it works fine.
    Thank you.

    I have also checked all the script files loaded by the page. And indeed
    there is one that seems to change onload. (A Google file it seems, aimed at
    measuring/optimizing map loading.) It is hard to tell what it does with its
    scrambled layout. When the client removed this js file, the problem was
    over. But your approach looks healhtier in this environment.
    Tom
     
    Tom de Neef, Feb 17, 2012
    #11
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.