473,406 Members | 2,378 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

How to do something BEFORE window.onload?

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

---------------------------------------------------

Jul 23 '05 #1
16 10083
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).
How?

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

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

Jul 23 '05 #2
>> 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).
How?

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

---------------------------------------------------

Jul 23 '05 #3
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.

Jul 23 '05 #4
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

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #5
>> 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.

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

---------------------------------------------------

Jul 23 '05 #6
Konrad Viltersten wrote:
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).
How?

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


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.

Jul 23 '05 #7
Random wrote:
<snip>
... , 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.
Jul 23 '05 #8
Richard Cornford wrote:
Random wrote:
<snip>
... , 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.


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.

Jul 23 '05 #9
Random wrote:
Konrad Viltersten wrote:
> 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).

> How?
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".


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.

Jul 23 '05 #10
Random wrote:
Richard Cornford wrote:
Random wrote:
<snip>
... , 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>

<snip> 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.
Jul 23 '05 #11
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.

Jul 23 '05 #12
>> 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

---------------------------------------------------

Jul 23 '05 #13
VK
>> Until each image header is received,
browser has no means to know the required offsetTop

after that:
it is very possible


.... as long as the page from cache is still matching the page on your
server.

Jul 23 '05 #14
JRS: In article <11*********************@g49g2000cwa.googlegroups. com>,
dated Fri, 3 Jun 2005 15:01:01, seen in news:comp.lang.javascript,
Random <ra*******@gmail.com> posted :


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.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME ©
Web <URL:http://www.uwasa.fi/~ts/http/tsfaq.html> -> Timo Salmi: Usenet Q&A.
Web <URL:http://www.merlyn.demon.co.uk/news-use.htm> : about usage of News.
No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.
Jul 23 '05 #15
Richard Cornford wrote, amongst other things:
Random wrote:
Richard Cornford wrote:
Random wrote:
<snip>
... , 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> <snip>
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.


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


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.

Jul 23 '05 #16
"Random" <ra*******@gmail.com> writes:
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'
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Brian | last post by:
Hi everyone, I'm writing a function (in javascript) that needs to do one thing if the page has not loaded, and another (different) thing if the page has already loaded. I'm looking for a way...
2
by: Jenny | last post by:
In the code below, I can write html content var t='<body BGCOLOR=blue>' for a new window. But if it contains javascript, such as var t='<body onload="window.open('new1.html')">', this code will...
4
by: jadiyo | last post by:
Hi, I've read many questions about how to automatically open a new window from a page using the JavaScript onLoad command. Unfortunately, due to the web application framework being used...
3
by: Liming | last post by:
Hi, Here is my situation. I have a textarea on the page. When the page loads, I need it to have some default text (which will be generated dynamically) so I did something like this ...
3
by: Frances | last post by:
I have three functions I need triggered when page loads, so have <body onload="function1();function2();function3()"> but I want to take all these function calls out of body tag and call them...
5
by: tuxedo | last post by:
The way the <body onload="something()"works ensures that the complete html document is loaded before something() is executed. Can the same be achieved when placing the onload call in document...
6
by: Daz | last post by:
Hello everyone, I would like to open a child window from the parent, and add an onload event listener to the child window which will tell the parent when the document has loaded. As far as I...
20
by: Mark Anderson | last post by:
Hi, I have this in an external JS library: ///////////////////////// function addMyEvent(){ var obj; if(document.attachEvent) { obj = document.getElementsByTagName('img'); for...
5
by: lilOlMe | last post by:
Hi there! I'm developing some crazy Tab Control in .NET that uses JavaScript. A particular JavaScript method needs to be called during the window.onload event in order to initialize my Tab...
2
by: Beni Rose | last post by:
I'm trying to open a new window using window.open and then print that window once it's loaded. It works fine in Firefox, but not at all in IE. No matter what I put in my onload, it gets ignored....
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.