image object, onload, onerror, recursive binary search

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • bjarthur

    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_g uts4(filename) {
    if((low+1)!=hig h) {
    last_num=(low+h igh)/2;
    my_img=new Image();
    my_img.onload = function() { find_last_img_g uts3(filename); };
    my_img.onerror = function() { find_last_img_g uts2(filename); };
    my_img.src=file name+"/"+last_num+".jp g"; }
    else {
    last_num=low; } }

    function find_last_img_g uts3(filename) {
    low=last_num;
    find_last_img_g uts4(filename); }

    function find_last_img_g uts2(filename) {
    high=last_num;
    find_last_img_g uts4(filename); }

    function find_last_img_g uts1(filename) {
    low=last_num;
    last_num*=2;
    my_img=new Image();
    my_img.onload = function() { find_last_img_g uts1(filename); };
    my_img.onerror = function() { find_last_img_g uts2(filename); };
    my_img.src=file name+"/"+last_num+".jp g"; }

    function find_last_img(f ilename) {
    last_num=1;
    find_last_img_g uts1(filename); }
    </SCRIPT>
    </HEAD>
    <BODY>
    <SCRIPT>
    find_last_img(" test.dir");
    document.write( "<A
    HREF='javascrip t:alert(last_nu m);'>alert(last _num)<\/A>");
    </SCRIPT>
    </BODY>
    </HTML>

  • Randell D.

    #2
    Re: image object, onload, onerror, recursive binary search

    bjarthur wrote:
    [color=blue]
    > 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_g uts4(filename) {
    > if((low+1)!=hig h) {
    > last_num=(low+h igh)/2;
    > my_img=new Image();
    > my_img.onload = function() { find_last_img_g uts3(filename); };
    > my_img.onerror = function() { find_last_img_g uts2(filename); };
    > my_img.src=file name+"/"+last_num+".jp g"; }
    > else {
    > last_num=low; } }
    >
    > function find_last_img_g uts3(filename) {
    > low=last_num;
    > find_last_img_g uts4(filename); }
    >
    > function find_last_img_g uts2(filename) {
    > high=last_num;
    > find_last_img_g uts4(filename); }
    >
    > function find_last_img_g uts1(filename) {
    > low=last_num;
    > last_num*=2;
    > my_img=new Image();
    > my_img.onload = function() { find_last_img_g uts1(filename); };
    > my_img.onerror = function() { find_last_img_g uts2(filename); };
    > my_img.src=file name+"/"+last_num+".jp g"; }
    >
    > function find_last_img(f ilename) {
    > last_num=1;
    > find_last_img_g uts1(filename); }
    > </SCRIPT>
    > </HEAD>
    > <BODY>
    > <SCRIPT>
    > find_last_img(" test.dir");
    > document.write( "<A
    > HREF='javascrip t:alert(last_nu m);'>alert(last _num)<\/A>");
    > </SCRIPT>
    > </BODY>
    > </HTML>
    >[/color]


    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

    Comment

    Working...