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. Advertising

  2. Tom de Neef

    Evertjan. Guest

    Tom de Neef wrote on 15 feb 2012 in comp.lang.javascript:

    > 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?


    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.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
    Evertjan., Feb 14, 2012
    #2
    1. Advertising

  3. Tom de Neef

    Tom de Neef Guest

    "Evertjan." <> schreef in bericht
    news:Xns9FFA4EC91084eejj99@194.109.133.133...
    > Tom de Neef wrote on 15 feb 2012 in comp.lang.javascript:
    >
    >> 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?

    >
    > When JS has encountered an error,
    > preventing correct parsing of getChartInfo() ?
    >
    >
    > Try:
    > <script type='text/javascript'>
    > blah(; // missing ")"
    > function a(){alert('ok');};
    > </script>
    > <body onload='a();'>
    >


    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. On Wed, 15 Feb 2012 09:26:19 +0100, Tom de Neef wrote:

    > 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


    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

    "Denis McMahon" <> schreef in bericht
    news:4f3b761c$0$29584$...
    > On Wed, 15 Feb 2012 09:26:19 +0100, Tom de Neef wrote:
    >
    >> 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

    >
    > 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.


    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:

    > "Evertjan." <> schreef in bericht
    > news:Xns9FFA4EC91084eejj99@194.109.133.133...
    >> Tom de Neef wrote on 15 feb 2012 in comp.lang.javascript:
    >>
    >>> 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?

    >>
    >> When JS has encountered an error,
    >> preventing correct parsing of getChartInfo() ?
    >>
    >>
    >> Try:
    >> <script type='text/javascript'>
    >> blah(; // missing ")"
    >> function a(){alert('ok');};
    >> </script>
    >> <body onload='a();'>
    >>

    >
    > 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.


    Why didn't you tell about which browser and what errors?

    On my Chrome my example acts like you posted.

    > I have now checked the attributes and properties of the body element:
    > onload is empty.


    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


    > 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?


    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.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
    Evertjan., Feb 15, 2012
    #6
  7. On Wed, 15 Feb 2012 10:26:15 +0100, Tom de Neef wrote:

    > "Denis McMahon" <> schreef in bericht
    > news:4f3b761c$0$29584$...
    >> On Wed, 15 Feb 2012 09:26:19 +0100, Tom de Neef wrote:
    >>
    >>> 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

    >>
    >> 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.

    >
    > 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.


    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. On Wed, 15 Feb 2012 00:09:57 +0100, Tom de Neef wrote:

    >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


    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
    all.nl>, Wed, 15 Feb 2012 00:09:57, Tom de Neef <>
    posted:

    >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()">.


    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.

    > ...
    >In a test environmet
    > ...



    --
    (c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
    Web <http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
    Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
    Do not Mail News to me. Before a reply, quote with ">" or "> " (SonOfRFC1036)
    Dr J R Stockton, Feb 15, 2012
    #9
  10. On Wed, 15 Feb 2012 20:37:39 +0000, Dr J R Stockton
    <> wrote:

    >In comp.lang.javascript message <4f3ae9c5$0$6918$4
    >all.nl>, Wed, 15 Feb 2012 00:09:57, Tom de Neef <>
    >posted:
    >
    >>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()">.

    >
    >Try <body onload="alert(12345);getChartInfo()">.
    >
    >And scan all files, HTML and JS, for any other occurrences of onload
    >which might have overridden that.


    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

    "Hans-Georg Michna" <> schreef in bericht
    news:...
    > On Wed, 15 Feb 2012 00:09:57 +0100, Tom de Neef wrote:
    >
    > 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>


    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. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Fabio R.
    Replies:
    0
    Views:
    770
    Fabio R.
    Mar 25, 2005
  2. =?Utf-8?B?Q2FsdmluIEtE?=

    <body onload="alert('Hello');"> does not work! Why?

    =?Utf-8?B?Q2FsdmluIEtE?=, Aug 25, 2005, in forum: ASP .Net
    Replies:
    7
    Views:
    10,388
    =?Utf-8?B?Q2FsdmluIEtE?=
    Aug 29, 2005
  3. Mel
    Replies:
    5
    Views:
    152
    Dr John Stockton
    Jan 16, 2004
  4. David Otton

    window.onload and body.onload differences

    David Otton, Nov 4, 2004, in forum: Javascript
    Replies:
    2
    Views:
    522
    Martin Honnen
    Nov 4, 2004
  5. DrKen
    Replies:
    8
    Views:
    1,773
    Denis McMahon
    Jun 24, 2011
Loading...

Share This Page