onload in Safari

T

Tim Streater

I thought that if I have:

....
<body onload="some_script();">
....

that some_script would not be called until the <body> was completely
loaded - is this not the case? With Safari 1.3 I seem to have to delay
inside some_script (there is some php in the <body> that slows down the
loading). Since I happen to have a spare iframe in my <body>, I load a
tiny bit of html in it whose job is simply to set a "loaded" flag,
tested inside my delay code.

What I was observing was that some fields inside a <form> in the <body>,
whose values are set by some_script, were, with Safari, not visible
until I clicked in one of them - then they all popped into sight. I
wasn't seeing this with other browsers and a delay mechanism fixed it.

It was as if the onload was triggered as soon as it was encountered
rather than when the loading was complete.

-- tim
 
B

bmgz

David said:
<script> elements are allowed inside <head> elements and <body> elements,
but not outside them.
whoops,
Why not rather put the <script> at the end of the page BEFORE </body>?
 
A

ASM

David said:
<script> elements are allowed inside <head> elements and <body> elements,
but not outside them.

*not allowed* doesn't mean *forbidden* ;-)

script written after </html> is interpreted ... so ...
 
A

ASM

David said:
So you are depending on browsers to fix your mistakes for you. Which isn't a
good idea.

Any way we are talking about JS so ... the dependance ... it'd got :-/
 
L

Lasse Reichstein Nielsen

ASM said:
*not allowed* doesn't mean *forbidden* ;-)

It doesn't mean "illegal" (the police won't come for you), but it does
mean "invalid" HTML.
script written after </html> is interpreted ... so ...

In some browsers. Maybe even in all existing browsers (but none of us have
tested that, since "all existing browsers" is quite a lot).

Howver, when you use invalid HTML, you depend on all browsers, unknown
and future, to treat the HTML (and DOM and other scripting issues) the
same way as the browsers you have tested with.

If you use valid HTML, you just depend on unknown and future browsers
to follow the standard. That is quite a lot more likely (perhaps with
future versions of IE as an exception :).

/L
 
R

RobG

Tim said:
I thought that if I have:

...
<body onload="some_script();">
...

that some_script would not be called until the <body> was completely
loaded - is this not the case? With Safari 1.3 I seem to have to delay
inside some_script (there is some php in the <body> that slows down the
loading). Since I happen to have a spare iframe in my <body>, I load a
tiny bit of html in it whose job is simply to set a "loaded" flag,
tested inside my delay code.

What I was observing was that some fields inside a <form> in the <body>,
whose values are set by some_script, were, with Safari, not visible
until I clicked in one of them - then they all popped into sight. I
wasn't seeing this with other browsers and a delay mechanism fixed it.

It was as if the onload was triggered as soon as it was encountered
rather than when the loading was complete.

Got a link or sample?

Maybe use setTimeout() to run the script say 50 milliseconds after
onload (though that seems kinda kludgy), something like:

if ( theForm ) {
// run script
} else {
// run using setTimeout()
}
 
B

bmgz

RobG said:
Maybe use setTimeout() to run the script say 50 milliseconds after
onload (though that seems kinda kludgy), something like:

if ( theForm ) {
// run script
} else {
// run using setTimeout()
}

By trying to guess the time it takes to load the page, you are dealing
with a completly unknown, unpredictable quatity - kinda like saying how
long is a piece of string. Kludgy? -more like impossible.
 
T

Tim Streater

RobG said:
Got a link or sample?

Maybe use setTimeout() to run the script say 50 milliseconds after
onload (though that seems kinda kludgy), something like:

if ( theForm ) {
// run script
} else {
// run using setTimeout()
}

Here's a (cut-down) sample:

<html><head>
<script type="text/javascript">

function some_script ()
{
document.forms[0].formitem.value = "some text";
}
</script>
</head>
<body class=mtext onload="some_script();">

<form><input readonly name="formitem" type=text size=20></form>

<?

(here is lots of PHP which reads stuff from a mysql database and
eventually echoes some stuff)

?>

<iframe blah blah blah>
</body></html>


I guess I could put some_script just before the </body>.

What I did in the end was to cause some_script to start a delay, which
when complete checked a flag and either restarted the delay or called
some_script again.

The flag is set inside the <iframe> I happen to have available.

But I'm less concerned about the precise details of a workaround, as
understanding just what the onload for a <body> is supposed to do. The
docs I have insist it fires after loading is complete, but I am no
convinced.

-- tim
 
R

RobG

Tim Streater wrote:
[...]
Here's a (cut-down) sample:

<html><head>
<script type="text/javascript">

function some_script ()
{
document.forms[0].formitem.value = "some text";
}
</script>
</head>
<body class=mtext onload="some_script();">

<form><input readonly name="formitem" type=text size=20></form>

<?

(here is lots of PHP which reads stuff from a mysql database and
eventually echoes some stuff)

?>

<iframe blah blah blah>
</body></html>


I guess I could put some_script just before the </body>.

I doubt that would help, it should be parsed & start running before
the body onload is run.
What I did in the end was to cause some_script to start a delay, which
when complete checked a flag and either restarted the delay or called
some_script again.

The flag is set inside the <iframe> I happen to have available.

But I'm less concerned about the precise details of a workaround, as
understanding just what the onload for a <body> is supposed to do. The
docs I have insist it fires after loading is complete, but I am no
convinced.

Sift through these:
<URL:http://www.quirksmode.org/bugreports/archives/safari/index.html>

or report it as a bug to Apple (you have to create an ADC account, but
it's free and they don't bug you ..).

<URL:http://webkit.opendarwin.org/quality/reporting.html>
 

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

Forum statistics

Threads
473,995
Messages
2,570,225
Members
46,815
Latest member
treekmostly22

Latest Threads

Top