image object, onload, onerror, recursive binary search

B

bjarthur

i have (see below) what i think is a fairly simple algorithm, but yet
it doesn't work. given a directory with consecutively numbered jpeg
files (1.jpg, 2.jpg, 3.jpg...), it is designed to count how many files
there are using a binary search. it uses the onload and onerror event
handlers of the image object to see if the files successfully loaded or
not. testing on a macintosh (os x 10.3.8) with a directory of 39
files, IE 5.2 and firefox 1.0 both give the correct answer of 39.
strangely, two other gecko variants (mozilla 1.7.5 and camino 0.8.2)
give 64: for some reason the onerror call does not work properly.
safari 1.2.4 gives infinity. anyone have any ideas?

thanks in advance.

ben



<HTML>
<HEAD>
<SCRIPT>
var my_img, last_num, low, high;

function find_last_img_guts4(filename) {
if((low+1)!=high) {
last_num=(low+high)/2;
my_img=new Image();
my_img.onload = function() { find_last_img_guts3(filename); };
my_img.onerror = function() { find_last_img_guts2(filename); };
my_img.src=filename+"/"+last_num+".jpg"; }
else {
last_num=low; } }

function find_last_img_guts3(filename) {
low=last_num;
find_last_img_guts4(filename); }

function find_last_img_guts2(filename) {
high=last_num;
find_last_img_guts4(filename); }

function find_last_img_guts1(filename) {
low=last_num;
last_num*=2;
my_img=new Image();
my_img.onload = function() { find_last_img_guts1(filename); };
my_img.onerror = function() { find_last_img_guts2(filename); };
my_img.src=filename+"/"+last_num+".jpg"; }

function find_last_img(filename) {
last_num=1;
find_last_img_guts1(filename); }
</SCRIPT>
</HEAD>
<BODY>
<SCRIPT>
find_last_img("test.dir");
document.write("<A
HREF='javascript:alert(last_num);'>alert(last_num)<\/A>");
</SCRIPT>
</BODY>
</HTML>
 
R

Randell D.

bjarthur said:
i have (see below) what i think is a fairly simple algorithm, but yet
it doesn't work. given a directory with consecutively numbered jpeg
files (1.jpg, 2.jpg, 3.jpg...), it is designed to count how many files
there are using a binary search. it uses the onload and onerror event
handlers of the image object to see if the files successfully loaded or
not. testing on a macintosh (os x 10.3.8) with a directory of 39
files, IE 5.2 and firefox 1.0 both give the correct answer of 39.
strangely, two other gecko variants (mozilla 1.7.5 and camino 0.8.2)
give 64: for some reason the onerror call does not work properly.
safari 1.2.4 gives infinity. anyone have any ideas?

thanks in advance.

ben



<HTML>
<HEAD>
<SCRIPT>
var my_img, last_num, low, high;

function find_last_img_guts4(filename) {
if((low+1)!=high) {
last_num=(low+high)/2;
my_img=new Image();
my_img.onload = function() { find_last_img_guts3(filename); };
my_img.onerror = function() { find_last_img_guts2(filename); };
my_img.src=filename+"/"+last_num+".jpg"; }
else {
last_num=low; } }

function find_last_img_guts3(filename) {
low=last_num;
find_last_img_guts4(filename); }

function find_last_img_guts2(filename) {
high=last_num;
find_last_img_guts4(filename); }

function find_last_img_guts1(filename) {
low=last_num;
last_num*=2;
my_img=new Image();
my_img.onload = function() { find_last_img_guts1(filename); };
my_img.onerror = function() { find_last_img_guts2(filename); };
my_img.src=filename+"/"+last_num+".jpg"; }

function find_last_img(filename) {
last_num=1;
find_last_img_guts1(filename); }
</SCRIPT>
</HEAD>
<BODY>
<SCRIPT>
find_last_img("test.dir");
document.write("<A
HREF='javascript:alert(last_num);'>alert(last_num)<\/A>");
</SCRIPT>
</BODY>
</HTML>


I'm a newbie with a few months of js under my belt, and was interested
to find the onload and onerror checks that you could do on the image...
However my JavaScript O'Reilly guide also has a 'complete' test that you
could try, perhaps as an additional test - it works on IE and Mozilla
1.7.5 the last time I tested it.

Does this help any?

randelld
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top