body onload not executed

T

Tom de Neef

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
 
E

Evertjan.

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();'>
 
T

Tom de Neef

Evertjan. said:
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 ")"
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
 
D

Denis McMahon

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
 
T

Tom de Neef

Denis McMahon said:
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
 
E

Evertjan.

Tom de Neef wrote on 15 feb 2012 in comp.lang.javascript:
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..
 
D

Denis McMahon

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
 
H

Hans-Georg Michna

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
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]
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.
 
G

Gene Wirchenko

In comp.lang.javascript message <[email protected]


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
 
T

Tom de Neef

Hans-Georg Michna said:
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
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,831
Latest member
HealthSmartketoReviews

Latest Threads

Top