How to do something BEFORE window.onload?

  • Thread starter Konrad Viltersten
  • Start date
K

Konrad Viltersten

I have a working script but it gets executed first after the
page has finished loading all the images (which may take a
while). Since the script is placed in window.onload i
understand it has to wait for the loading to get done.

However, i wish to execute the script BEFORE all the
images had time to get loaded. Is that doable? How?

--

Vänligen
Konrad
---------------------------------------------------

Sleep - thing used by ineffective people
as a substitute for coffee

Ambition - a poor excuse for not having
enough sense to be lazy

---------------------------------------------------
 
V

VK

Is that doable?
It depends on what is your script doing. If it addresses any page
elements, it will be a dice game (50/50 the requested element is
rendered or not).
Just insert your script at the point you want to start the execution.

<body>
......
<script type="text/javascript">
// your script
</script>
....
 
K

Konrad Viltersten

Is that doable?
It depends on what is your script doing. If it addresses any page
elements, it will be a dice game (50/50 the requested element is
rendered or not).

Just insert your script at the point you want to start the
execution.

<body>
.....
<script type="text/javascript">
// your script
</script>
...


The thing is that i never execute the code. It is put in the
function "window.onload" and placed in the head of the
document. BUT the code is not executed "onload" as in
"as we get to the page" - it's more "onload" as in "when
we have finished downloading the whole thing".

The loading time is of importance here since the browser
loads about 100 images. I wish to do a thing BEFORE all
the images have been loaded.

Did i make myself more clear or confused even more? :)

Here's an example. Try to scroll a bit, exit, then come
back again to the page and you'll be automatically scrolled
to the position you were at when you left.

The issue is that you have to wait until all the images has
been loaded, which takes a while.
http://www.viltersten.com/sve/files/elvis.htm

--

Vänligen
Konrad
---------------------------------------------------

Sleep - thing used by ineffective people
as a substitute for coffee

Ambition - a poor excuse for not having
enough sense to be lazy

---------------------------------------------------
 
B

Botan Guner

I think that your problem is loading the external images. It takes too
much time to load all the images from another site and its still
loading :)

You can load the images after loading the page with XMLHttpRequest()...
that you will execute your script after loading the page.
 
M

Michael Winter

On 03/06/2005 11:30, Konrad Viltersten wrote:

[snip]
[...] the code is not executed "onload" as in "as we get
to the page" - it's more "onload" as in "when we have
finished downloading the whole thing".

That's generally how the load event is interpreted.
The loading time is of importance here since the browser
loads about 100 images. I wish to do a thing BEFORE all
the images have been loaded.

Then include the SCRIPT element as the last child of the BODY element.
The script will be parsed and executed more-or-less when the entire
document has been loaded, irrespective of whether the images have been
downloaded.

[snip]

Mike
 
K

Konrad Viltersten

The loading time is of importance here since the browser
Then include the SCRIPT element as the last child of the BODY
element. The script will be parsed and executed more-or-less when
the entire document has been loaded, irrespective of whether the
images have been downloaded.


Hmmm... I simply didn't think of it. I'll try that and we'll
see what will happen.

Thanks to both of you, guys.

--

Vänligen
Konrad
---------------------------------------------------

Sleep - thing used by ineffective people
as a substitute for coffee

Ambition - a poor excuse for not having
enough sense to be lazy

---------------------------------------------------
 
R

Random

Konrad said:
The thing is that i never execute the code. It is put in the
function "window.onload" and placed in the head of the
document. BUT the code is not executed "onload" as in
"as we get to the page" - it's more "onload" as in "when
we have finished downloading the whole thing".

I think what VK was saying here is essentially valid:
Since you want the code executed after the HTML has been loaded, but
not necessarily waiting for all referenced objects to be loaded (i.e.
pictures, sounds), just put the code at the bottom of the document.

</body>
<script> ... </script>
</html>

And don't use onLoad -- just execute the code yourself, within that
<script /> element.
 
R

Richard Cornford

Random wrote:
... , just put the code at the bottom of the document.

</body>
<script> ... </script>
</html>
<snip>

More predictable results would likely be achieved by avoiding a
dependency on browser error-correction, so using valid HTML:-

<script type="text/javascript"> ... </script>
</body>
</html>

Where the script element has the required type attribute and appears in
a context in which script elements are allowed.

Richard.
 
R

Random

Richard said:
Random wrote:

<snip>

More predictable results would likely be achieved by avoiding a
dependency on browser error-correction, so using valid HTML:-

<script type="text/javascript"> ... </script>
</body>
</html>

Where the script element has the required type attribute and appears in
a context in which script elements are allowed.

Richard.

Intended to relate the basic idea only. You'll notice it was very
clearly a fragment. As well, if someone grabs code from anywhere and
doesn't adapt it to his own needs / desires, he's making his own bed,
no?

I shouldn't have posted at all, but Google Groups didn't show me the
two preceding posts until I'd already posted mine. Surprise surprise.
 
R

Random

Random said:
I think what VK was saying here is essentially valid:
Since you want the code executed after the HTML has been loaded, but
not necessarily waiting for all referenced objects to be loaded (i.e.
pictures, sounds), just put the code at the bottom of the document.

</body>
<script> ... </script>
</html>

And don't use onLoad -- just execute the code yourself, within that
<script /> element.


For God's sake, Google Groups sucks... it didn't show me the two posts
that basically resolved this one.

Sorry about that.
 
R

Richard Cornford

Intended to relate the basic idea only.

A basic idea expressed in words could only be subject to a consideration
of the worth of the idea, but expressed in code it becomes more concrete
and so subject to technical correction. The idea of putting a script
element in to a context where script elements are not allowed is wrong
and should not go uncorrected.
You'll notice it was very clearly a fragment.

The smaller the fragment the fewer the opportunities for errors. Four
tags, two errors resulting in invalid mark-up, not an encouraging ratio.
As well, if someone grabs code from anywhere and
doesn't adapt it to his own needs / desires, he's
making his own bed, no?

So does this mean that you expect to be able to post any old nonsense in
response to questions here and consider anyone taking you seriously as
being at fault for taking you seriously?

The nature of your 'basic idea' was that placing javascript code inside
the suggested script elements in the suggested context would represent
the required adaptation.
I shouldn't have posted at all, but Google Groups didn't
show me the two preceding posts until I'd already posted
mine. Surprise surprise.

Google may have downgraded its Usenet interface to the point where it is
on a par with the very worst available but they cannot be considered
responsible for the fact that Usenet communication is not instantaneous.
It takes time for posts to propagate globally, hours usually, days
sometimes.

Richard.
 
D

Dr John Stockton

JRS: In article <[email protected]>,
dated Fri, 3 Jun 2005 15:01:01, seen in
Random said:
For God's sake, Google Groups sucks... it didn't show me the two posts
that basically resolved this one.


Don't use it, then; use a proper newsreader.
 
V

VK

be automatically scrolled to the position you were at when you left

This partucular task in your particular case is not possible. Browser
gets image size from width/height attributes of image tag. If these
attributes are not indicated, then browser has to examine each image
header to retrieve the data. Until each image header is received,
browser has no means to know the required offsetTop for the page, so
any scroll operations are pointless.

And just saving your time for another "Super FAQ" post:

- "Can I do for each image <image src=... width="10" height="10"
onLoad="studyImageHeaderAndSetRealSize"> "
- No, you cannot.
 
K

Konrad Viltersten

be automatically scrolled to the position you were at when you left
This partucular task in your particular case is not possible.
Browser gets image size from width/height attributes of image tag.
If these attributes are not indicated, then browser has to examine
each image header to retrieve the data. Until each image header is
received, browser has no means to know the required offsetTop for
the page, so any scroll operations are pointless.


Actually, it is very possible (allthough you had no way to
know that since you surely have much more interesting
things to do than checking my page). :)

I actually know for sure the size of all the images since
it's a bunch of comic strips, so in this particular case, it's
fully doable. However, i'll take your words of advice and
"never" attempt a try like that.

Thanks!

--

Vänligen
Konrad
---------------------------------------------------

Sleep - thing used by ineffective people
as a substitute for coffee

Ambition - a poor excuse for not having
enough sense to be lazy

---------------------------------------------------
 
R

Random

Richard Cornford wrote, amongst other things:
A basic idea expressed in words could only be subject to a consideration
of the worth of the idea, but expressed in code it becomes more concrete
and so subject to technical correction.

A matter of perspective. I would imagine that an idea is subject to
correction regardless of how it is expressed. Applying differing
standards to differing forms of expression is a personal choice, and it
would be wasted effort to assail such a choice.

I will say that whether words or code, if an expression is very clearly
a fragment then it is also clear that it is not intended to work as-is,
and I at least would think that corrections to make it less of a
fragment (as opposed corrections to the idea that the fragment
illustrates) on the basis that as-is it will not work, are also wasted
effort.

But then again, so is justifying oneself. Since I have no desire to try
to change your mind regarding any of this, I'll leave it at that.
The smaller the fragment the fewer the opportunities for errors. Four
tags, two errors resulting in invalid mark-up, not an encouraging ratio.

See above. The degree to which the fragment is or is not fragmentary is
irrelevant when it is clearly intended as a fragment.
So does this mean that you expect to be able to post any old nonsense in
response to questions here and consider anyone taking you seriously as
being at fault for taking you seriously?

Yes and no. Every person is at fault for what they choose or choose not
to do. Every person is responsible for his own actions, and I would no
more consider myself responsible for the consequences of someone
failing to observe what I have made clear than I would for the homeless
man down the road responsibly spending the dollar I have given him.

That said, of course it is hoped that one would make an effort to be
honest and correct, regardless of the forum or medium, and I certainly
do. If I have doubts about whether my code will work, I test it first,
and/or express those doubts in the post so that the reader may be
forewarned. Of course I make mistakes, but humans do that. This was not
one of those mistakes-- responding to your reply was.

Are you saying that you hold other people responsible when you reuse
their code without evaluating it first? It's a rhetorical question. I
have no interest in the answer.

Perhaps a disclaimer at the bottom of every post:
*May not work in all implementations of all levels of the DOM on all
platforms, or of JavaScript or derivative languages, or all versions or
implementations of HTML or similar languages or other any other
document type. The Author is not responsible for damages resulting from
use of the information contained herein, including but not limited to
loss of time, loss of data, and loss of potential income. The
information contained herein is provided as-is, for educational
purposes only, without warranty, express or implied, especially but not
exclusively with regard to useability or merchantability. Some
jurisdictions do not allow implied warranties to be disclaimed, so this
clause may not apply to you. The reader is responsibile for all
consequences of reading this article or post, and for the use or
failure to use the information contained hereing.
The nature of your 'basic idea' was that

.... placing the script at the bottom of the document would allow it to
be executed in the order desired, which is correct. Lo, it works.


I will not make any further attempt to explain myself. If you have
questions or concerns, feel free to approach me with them as questions
or concerns, but a combative approach is unlikely to achieve the
desired result.

Such pendantry has very little innate value in most forums or media.
 
L

Lasse Reichstein Nielsen

Random said:
Such pendantry has very little innate value in most forums or media.

I think you are underestimating the complexities of writing for as
heterogenic an environment as "javascript in a browser". No matter
what you do, there is probably some browser somewhere that fails to
understand it correctly. The more you stray from the standards, the
more likely this is. That is why the regulars of this group might
seem pedantic when they recommend against non-valid HTML, and in
particular, when they (or "we" :) correct HTML errors in examples
given as suggested solutions, fragmentary or not. Believe me, it
is not pure pedantry.

Richard Cornford's original correction said nothing more than
that invalid HTML makes it less predictable what browsers will
do, and he gave an example that has the same expected behavior
while being valid HTML. It's hard to do *less* while advocating
valid HTML, which I too think is The Right Thing To Do(tm).

I also recommend reading
<URL:http://diveintomark.org/archives/2003/05/05/why_we_wont_help_you>

/L 'Me thinkst thou doest protest too much'
 

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,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top