checking whether preview pic loaded?

J

jodleren

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
 
T

Thomas 'PointedEars' Lahn

jodleren said:
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
 
J

jodleren

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 :)
 
J

Jeremy J Starcher

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.
 
T

Thomas 'PointedEars' Lahn

Thomas said:
[...]
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
 
W

wilq

  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';
  }

As far as I know this way of attaching events leaks memory, but of
course jodleren maybe don't need good solutions. Just explain how the
"img" will be removed by garbage collector.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,085
Messages
2,570,597
Members
47,218
Latest member
GracieDebo

Latest Threads

Top