From: jodleren on
Hello

I am updating a system, and want to check this line:

document.all.preview.src = 'Temp/' + ob.id +'.bmp';

by added
if not loaded then
document.all.preview.src = something else;

How do I check whether the picture loaded?

WBR
Sonnich
From: Thomas 'PointedEars' Lahn on
jodleren wrote:

> I am updating a system, and want to check this line:
>
> document.all.preview.src = 'Temp/' + ob.id +'.bmp';

Don't; that's proprietary IE/MSHTM nonsense. Should be

document.images["preview"].src = 'Temp/' + ob.id +'.png';

(The `img' element should have the name `preview', although ID `preview'
is supported by standards-compliant browsers, too.)

As a nice side effect, it is going to work outside of Windows and Internet
Explorer regardless of layout mode.

> by added
> if not loaded then
> document.all.preview.src = something else;
>
> How do I check whether the picture loaded?

Try this quickhack (untested; partially proprietary too, but that part
should work everywhere where client-side scripting is supported, for
historical reasons):

/*
* Helper functions; see <http://PointedEars.de/scripts/dhtml.js>
* for the fully featured versions
*/
function _addEventListener(o, sEvent, fListener)
{
if (!o || !sEvent || !fListener) return;

/* see isMethod() for a more precise feature test */
if (typeof o.addEventListener == "function")
{
/* W3C DOM Level 2, 3-WD Events compliant */
o.addEventListener(sEvent, fListener, false);
}
else
{
/* proprietary: IE/MSHTML a.o. */
o["on" + sEvent.toLowerCase()] = fListener;
}
}

function _removeEventListener(o, sEvent, fListener)
{
if (!o || !sEvent || !fListener) return;

/* see isMethod() for a more precise feature test */
if (typeof o.removeEventListener == "function")
{
/* W3C DOM Level 2, 3-WD Events compliant */
o.removeEventListener(sEvent, fListener, false);
}
else
{
/* proprietary: IE/MSHTML a.o. */
o["on" + sEvent.toLowerCase()] = null;
}
}

/* End of helper functions */

var img = document.images["preview"];
if (img)
{
var f =

_addEventListener(img, "error",
function() {
/* Don't error out if the replacement image doesn't load */
_removeEventListener(this, "error", arguments.callee);

this.src = "...";
});

img.src = 'Temp/' + ob.id + '.png';
}
else
{
/* DEBUG */
}

Please read the FAQ before posting here: <http://jibbering.com/faq/#posting>


PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f806at$ail$1$8300dec7(a)news.demon.co.uk>
From: jodleren on

> I am updating a system, and want to check this line:
>   document.all.preview.src = 'Temp/' + ob.id +'.bmp';
> by added
>   if not loaded then
>      document.all.preview.src = something else;
> How do I check whether the picture loaded?

I looked around, and came up with this idea:

img1=new Image;
img1.src='Temp/' + ob.id +'.bmp';
i=1000;
while(!img1.complete && (i>0)) i--;
if(img1.complete)
document.all.preview.src=img1.src;
else
window.status='didnt work';

Nope, it does not work
Sorry, I am not an JS expert, the idea could work :) but I need
something as sleep (which does not exist).
An onLoad event does not seem well in my application.

And yes, the idea is to replace those BMPs :)
From: Jeremy J Starcher on
On Wed, 21 Oct 2009 10:46:31 -0700, jodleren wrote:

>> I am updating a system, and want to check this line:
>>   document.all.preview.src = 'Temp/' + ob.id +'.bmp';
>> by added
>>   if not loaded then
>>      document.all.preview.src = something else;
>> How do I check whether the picture loaded?
>
> I looked around, and came up with this idea:
>
> img1=new Image;
> img1.src='Temp/' + ob.id +'.bmp';
> i=1000;
> while(!img1.complete && (i>0)) i--;
> if(img1.complete)
> document.all.preview.src=img1.src;
> else
> window.status='didnt work';
>
> Nope, it does not work
> Sorry, I am not an JS expert, the idea could work :) but I need
> something as sleep (which does not exist). An onLoad event does not seem
> well in my application.
>
> And yes, the idea is to replace those BMPs :)

Pay attention to Thomas's post in particular his comments about your code
only working with Internet Explorer. Pay attention to his entire post.
While he may not have said why he crossed every 't' and dotted every 'i',
he did so.

Forget that you ever heard about 'document.all' Wipe it from your mind,
as though it no longer exists. Use the other collections.

Don't touch the status bar. On many setups, Javascript can't modify its
contents.

From: Thomas 'PointedEars' Lahn on
Thomas 'PointedEars' Lahn wrote:

> [...]
> var img = document.images["preview"];
> if (img)
> {
> var f =

This harmless line is a leftover from refactoring. You can safely remove
it.

> _addEventListener(img, "error",
> function() {
> /* Don't error out if the replacement image doesn't load */
> _removeEventListener(this, "error", arguments.callee);
>
> this.src = "...";
> });
> [...]


PointedEars
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm> (404-comp.)