Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Testing Image Existence with side-effects

Reply
Thread Tools

Testing Image Existence with side-effects

 
 
Charles Harrison Caudill
Guest
Posts: n/a
 
      10-05-2005
I'm trying to write a function to determine whether or not an image exists.

Most people recommend setting the onload event handler. That works like a
charm except that side-effective actions seem to be impossible. I can, as
nearly as I can tell, run alert and opera.postError (I test on IE, firefox,
and opera by the way). I've tried using the 'this' while in the event
handler, setting this.name, test.name, window.document.getElementById(test's
id).name. I just can't seem to find any means of using side effects inside
the event handler.

Below are several examples of things I've tried so far:

----------------------------------------
var test = new Image();
var id = imageUtils_getUniqueId();
alert("before-> " + window.document.body.childNodes.length);
test.id = id;
test.name = id;
test.onload = function () {window.document.body.appendChild(test);};
test.src = url;
while(!test.complete){}
alert("after-> " + window.document.body.childNodes.length);

alert: before-> 12
alert: after-> 12
yet, the image still shows up on the page...
----------------------------------------

----------------------------------------
var test = new Image();
var didSucceed = false;
test.onload = function () {
didSucceed = true;
alert("load-> " + test.height);
};
test.src = url;
alert("post-> " + test.height);
return didSucceed;

alert: load-> 302
alert: post-> 0
returns false;
----------------------------------------

----------------------------------------
var test = new Image();
test.src = url;
while(!test.complete){}
alert(test.height);

alert: 0
----------------------------------------

----------------------------------------
var test = new Image();
test.id = imageUtils_getUniqueId();
window.document.body.appendChild(test);
test.src = url;
while(!test.complete){}
alert(test.height);

alert: 0
----------------------------------------

Any assistance would be greatly appreciated.

Thank you,
Harrison

--
Harrison Caudill BS BS | .^ www.hypersphere.org
Computer Science & Physics Graduate | | Me*Me=1
Georgia Institute of Technology | v' I'm just a normal guy
 
Reply With Quote
 
 
 
 
alu
Guest
Posts: n/a
 
      10-06-2005

"Charles Harrison Caudill" wrote
> I'm trying to write a function to determine whether or not an image

exists.
>
> Most people recommend setting the onload event handler. That works like a
> charm except that side-effective actions seem to be impossible. I can, as
> nearly as I can tell, run alert and opera.postError (I test on IE,

firefox,
> and opera by the way). I've tried using the 'this' while in the event
> handler, setting this.name, test.name,

window.document.getElementById(test's
> id).name. I just can't seem to find any means of using side effects

inside
> the event handler.
>
> Below are several examples of things I've tried so far:
>
> ----------------------------------------
> var test = new Image();
> var id = imageUtils_getUniqueId();
> alert("before-> " + window.document.body.childNodes.length);
> test.id = id;
> test.name = id;
> test.onload = function () {window.document.body.appendChild(test);};
> test.src = url;
> while(!test.complete){}
> alert("after-> " + window.document.body.childNodes.length);
>
> alert: before-> 12
> alert: after-> 12
> yet, the image still shows up on the page...
> ----------------------------------------
>
> ----------------------------------------
> var test = new Image();
> var didSucceed = false;
> test.onload = function () {
> didSucceed = true;
> alert("load-> " + test.height);
> };
> test.src = url;
> alert("post-> " + test.height);
> return didSucceed;
>
> alert: load-> 302
> alert: post-> 0
> returns false;
> ----------------------------------------
>
> ----------------------------------------
> var test = new Image();
> test.src = url;
> while(!test.complete){}
> alert(test.height);
>
> alert: 0
> ----------------------------------------
>
> ----------------------------------------
> var test = new Image();
> test.id = imageUtils_getUniqueId();
> window.document.body.appendChild(test);
> test.src = url;
> while(!test.complete){}
> alert(test.height);
>
> alert: 0
> ----------------------------------------
>
> Any assistance would be greatly appreciated.
>
> Thank you,
> Harrison



Well, I don't know if this is any assistance, but I can at least start an
argument....

I don't think checking for test.complete within a while() loop is a great
idea; the loop process will likely override the image loading attempt,
therefore defeating the purpose.
And of course, before it *is* complete, there is no way to get the height as
a test for existence, as your code suggests.
I probably don't understand your reasoning completely, but the following
test works:

var test = new Image();
test.src = url
window.document.body.appendChild(test);
while(document.images.length < 1){}
alert(document.images.length);

-alu


 
Reply With Quote
 
 
 
 
Harrison Caudill
Guest
Posts: n/a
 
      10-07-2005
I'm afraid it doesn't work. The results are the same if the image
exists or not. The problem is that the image loading process occurs
after the rest of the javascript on the page has been executed.
Consequently the only way I can find to detect the existence of an image
is to write every part of the html necessary after the image detection
needs to occur using javascript by running a

setTimeout('testThenWriteRestOfHTMLOrReschedule()' , 10);

or something like that.

Thanks though.

-harrison

PS This site doesn't seem to work properly in Opera. The submit button
doesn't appear.

::shrugs::


*** Sent via Developersdex http://www.developersdex.com ***
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Testing File Existence - best way? still just me Perl Misc 2 12-27-2007 03:57 PM
testing for existence of compilers/executables John Python 0 03-05-2006 04:36 PM
Checking fprintf for errors and testing for existence of a file Jim Hunter C Programming 3 03-06-2005 05:29 PM
Testing global variables for existence? Hadmut Danisch Ruby 2 12-16-2004 05:51 PM
testing for existence of an inline file with Inline::Files Sven Wolf Perl Misc 7 12-03-2004 10:17 PM



Advertisments