Delay counter

J

JellyON

Hi. Is there a way to delay a call to a page counter (ie. call to a
server script from an IMG tag) for the purpose to not lock the page
loading awaiting counter be displayed.

Maybe a setTimeout() launching an equivalent of "document.write", but
writing in a specific DIV ? Thanks in advance for your ideas.

Actually, the counter is called (without delay) like this :

<div id="count" style="position: absolute; visibility: visible; top:
20px; left: 20px; width: auto; height: auto; z-index: 5; cursor:
default"><img src="http://host.com/cgi-bin/counter.exe?color=red&font=
6"></div>
 
V

VK

JellyON said:
Hi. Is there a way to delay a call to a page counter (ie. call to a
server script from an IMG tag) for the purpose to not lock the page
loading awaiting counter be displayed.

Maybe a setTimeout() launching an equivalent of "document.write", but
writing in a specific DIV ? Thanks in advance for your ideas.

Actually, the counter is called (without delay) like this :

<div id="count" style="position: absolute; visibility: visible; top:
20px; left: 20px; width: auto; height: auto; z-index: 5; cursor:
default"><img src="http://host.com/cgi-bin/counter.exe?color=red&font=
6"></div>

Rendering delay happens not because of a server call by itself - but
because the engine cannot allocate the image until the image header
(with width and height) is received. As a result it cannot allocate
(render) the whole <div>.

If say it's a 128x32 image then this would solve the problem:
<div id="count" style="position: absolute; visibility: visible; top:
20px; left: 20px; width: auto; height: auto; z-index: 5; cursor:
default"><img src="http://host.com/cgi-bin/counter.exe?color=red&font=
6" width="128" height="32"></div>

If you server script is *very* slow (so user will see a red crossed
"missing image" sign for a while), you can fix the situation by:

<div id="count" style="position: absolute; visibility: visible; top:
20px; left: 20px; width: auto; height: auto; z-index: 5; cursor:
default"><img src="TempTransparent.gif" width="128" height="32"
onload="
if (this.src='TempTransparent.gif') {
this.src='http://host.com/cgi-bin/counter.exe?color=red&font= 6';
}"></div>
 
J

JellyON

! Nasty typo !
Read:
if (this.src=='TempTransparent.gif') {
this.src='http://host.com/cgi-bin/counter.exe?color=red&font= 6';
}">

Well, it sounds right :) I've just replaced the if by a

setTimeout(function(){this.src='http://host.com/cgi-bin/counter.exe?
color=red&font=6';});

because it's better for delaying counter call and I didn't seen the
reason why for testing "TempTransparent.gif" presence (obviously always
here). Maybe something I've missed ?

Well, now, it stay just a thing : I would like to display another DIV
(which contain a text like "Number of hits : ") only if counter call
succeeded. The question is not to know how to show the DIV (simply
change the visibility style attribute), but what to test. That is the
question !
 
V

VK

JellyON said:
Well, it sounds right :) I've just replaced the if by a

setTimeout(function(){this.src='http://host.com/cgi-bin/counter.exe?
color=red&font=6';});

because it's better for delaying counter call and I didn't seen the
reason why for testing "TempTransparent.gif" presence (obviously always
here). Maybe something I've missed ?

I did not mean to test for "TempTransparent.gif". The idea was to have
TempTransparent.gif loaded right away so user does not see empty image
frame. And right upon the TempTransparent.gif load you request the
counter picture so it appears on the page when ready. setTimeout is an
option but why make things more difficult as they should be? ;-)
Well, now, it stay just a thing : I would like to display another DIV
(which contain a text like "Number of hits : ") only if counter call
succeeded. The question is not to know how to show the DIV (simply
change the visibility style attribute), but what to test. That is the
question !

Error event supported by images since Netscape 3 - what else?

The combined code could be:
....
<img name="myCounter" src="myTransparent.gif" width="128" height="32"

onload="
if (this.src == 'myTransparent.gif') {
this.src='http://host.com/cgi-bin/counter.exe?color=red&font=6';
this.failedToLoad=false;
}
else {
document.getElementById('myDiv').style.visibility = 'visible';
}"

onerror="
this.src='myOops.gif';
this.failedToLoad=true;"
/>
....

As you cannot predict when exactly the counter will arrive, you better
let counter itself to make your <DIV> to appear, not vice versa.

To check the situation *later* (is counter loaded? is div displayed?)
you can always check the custom property failedToLoad:
if (document.images['myCounter'].failedToLoad) {
// I got some problems
}

Unlikely in your case, but be careful with animated gif's: they produce
new "load" event on each animation cicle (a very old GIF98a bug which
will never be fixed now).

Also make sure that at least 'myOops.gif' is always available,
otherwise you'll put your script into onerror loop. It also can be
fixed programmatically, but easier and better simply to have this image
on your server.
 
R

Randy Webb

JellyON said the following on 11/6/2005 5:34 AM:
Hi. Is there a way to delay a call to a page counter (ie. call to a
server script from an IMG tag) for the purpose to not lock the page
loading awaiting counter be displayed.

Display a transparent image temporarily. Use the window.onload (or body
onload) to trigger a function that will change the images src attribute
accordingly.
Maybe a setTimeout() launching an equivalent of "document.write", but
writing in a specific DIV ? Thanks in advance for your ideas.

Actually, the counter is called (without delay) like this :

<div id="count" style="position: absolute; visibility: visible; top:
20px; left: 20px; width: auto; height: auto; z-index: 5; cursor:
default"><img src="http://host.com/cgi-bin/counter.exe?color=red&font=
6"></div>


<img src="http://yourDomain.com/transparent.gif" name="counterImage"
width="##" height="##">

along with this script:

function changeCounterImage(){
document.images['counterImage'].src="http://host.com/cgi-bin/counter.exe?color=red&font=6";
}

window.onload = changeCounterImage;
 

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,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top