image object, onload, onerror, recursive binary search

Discussion in 'Javascript' started by bjarthur, Feb 23, 2005.

  1. bjarthur

    bjarthur Guest

    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>
    bjarthur, Feb 23, 2005
    #1
    1. Advertising

  2. bjarthur

    Randell D. Guest

    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
    Randell D., Feb 23, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Fabio R.
    Replies:
    0
    Views:
    792
    Fabio R.
    Mar 25, 2005
  2. Bogdan

    Binary tree search vs Binary search

    Bogdan, Oct 18, 2010, in forum: C Programming
    Replies:
    22
    Views:
    3,068
    Michael Angelo Ravera
    Oct 21, 2010
  3. Jürgen Heyn

    onError problem loading an image

    Jürgen Heyn, Feb 29, 2004, in forum: Javascript
    Replies:
    4
    Views:
    112
    Jürgen Heyn
    Feb 29, 2004
  4. Roger Shrubber
    Replies:
    1
    Views:
    111
    Richard Cornford
    Apr 21, 2004
  5. Is it onerror or onError?

    , Mar 21, 2008, in forum: Javascript
    Replies:
    12
    Views:
    318
Loading...

Share This Page