473,396 Members | 2,011 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,396 software developers and data experts.

image object, onload, onerror, recursive binary search

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>

Jul 23 '05 #1
1 6136
bjarthur wrote:
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
Jul 23 '05 #2

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

Similar topics

1
by: Nick | last post by:
How to change the pic to a "show error" image when loading images failed - for example, no image file exists? I tried to add the following statement in the onload event function: if...
2
by: Roger Withnell | last post by:
I browse for an image using form input type file and call a JavaScript function with onchange. I then want the function to check the image's properties (width, height, filesize). In WinIE,...
3
by: Dennis Allen | last post by:
Hi. I've been trying to write Javascript code that checking if an image exists. If it doesn't exist, I want to load a known image. I've tried: if (document.images) { var tester=new Image() //...
4
by: Dennis Allen | last post by:
Hi. I have one image that has to show up in a html page several times. To save time, I'd like to preload this image. As an example: if (document.images) { tester = new Image()...
5
by: joe | last post by:
Hi. Is there a way with Javascript to use a generic image if the one I want is not found? I'd appreciate if someone can post a code sample, or poit me to one. Thanks in advance.
1
by: quickcur | last post by:
Hi all, What happend to the onload event of <img src=myimage.jpgtag if the myimage.jpg is not available? My feeling is that the onload is not called at all. How can I capture the event if the...
2
by: balakrishnan.dinesh | last post by:
hi frnds, In my senario, My webpage will load dynamically from the server, Sometimes Images in my is not loading, how can i find , whether my image is loaded or not, through javascript. then...
1
by: Rick | last post by:
I consider myself a pretty good javascript coder, but I'm far from guru status and would appreciate feedback from some more experienced people. I came up with this idea for conditionally outputting...
4
by: Weston | last post by:
I've been playing around with some means of testing image loading, and noticing that the "complete" property seems to default to true. Trying the following in the squarefree javascript shell: I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
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.