Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #38641

Re: missing image error

Path csiph.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From Arno Welzel <usenet@arnowelzel.de>
Newsgroups comp.lang.javascript
Subject Re: missing image error
Date Tue, 4 Aug 2020 10:29:34 +0200
Lines 41
Message-ID <hoskjfFefplU1@mid.individual.net> (permalink)
References <mICdnUE2Q-twd7XCnZ2dnUU7-fvNnZ2d@westnet.com.au>
Mime-Version 1.0
Content-Type text/plain; charset=utf-8
Content-Transfer-Encoding 7bit
X-Trace individual.net LIcSxHfw1vjykNaitYIb6QoH3MMtrU65SxmINwZmS6FAJoo5tJ
Cancel-Lock sha1:3Lj/fRSOH3TjC7rMKTcOMvmjJGA=
In-Reply-To <mICdnUE2Q-twd7XCnZ2dnUU7-fvNnZ2d@westnet.com.au>
Xref csiph.com comp.lang.javascript:38641

Show key headers only | View raw


Andrew Poulos:

> I'm dynamically adding images to a page using
> 
> let img = new Image();
> 
> img.src = 'images/' + name + '.jpg';
> img.setAttribute("onerror", "this.onerror=null; 
> this.src='images/imagenotfound.png'");
> ...
> 
> and the page works as expected. That is, when the image is not available 
> 'imagenotfound' is shown in its place.
> 
> The client tests the page with the console open and each time an image 
> is unavailable they see a 404 error. I've tried explaining that it needs 
> to error before it can know to replace it with 'imagenotfound' but they 
> would prefer that image's 404 error not show.
> 
> Is there a way to trap the 404 so it doesn't show in the console?
> Or is there some other way I can try to get the image and thereby be 
> able to trap the 404?

No. That's the reason why there is a console - to see what is going on.
Without trying to load an image and getting the 404 error the "onerror"
attribute would not work at all.

The only way to avoid 404 errors would be a wrapper script on the
*server* which will either return the actual image or a placeholder if
the image can not be loaded:

<img src="server-side-loader?image=myimage.jpg" ...>

Where "server-side-loader" is a PHP script etc. which will try to load
the specificed image file and if this is not possible return the
placeholder.


-- 
Arno Welzel
https://arnowelzel.de

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

missing image error Andrew Poulos <ap_prog@hotmail.com> - 2020-08-04 14:58 +1000
  Re: missing image error Arno Welzel <usenet@arnowelzel.de> - 2020-08-04 10:29 +0200
    Re: missing image error James Kirk <noneya.invalid@gmail.com> - 2020-08-04 22:38 -0400
      Re: missing image error Arno Welzel <usenet@arnowelzel.de> - 2020-08-05 07:49 +0200
  Re: missing image error Julio Di Egidio <julio@diegidio.name> - 2020-08-04 02:18 -0700
    Re: missing image error Andrew Poulos <ap_prog@hotmail.com> - 2020-08-05 13:20 +1000
  Re: missing image error Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2020-08-05 08:44 +0200
    Re: missing image error Andrew Poulos <ap_prog@hotmail.com> - 2020-08-05 18:02 +1000
      Re: missing image error Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2020-08-05 10:18 +0200
      Re: missing image error Julio Di Egidio <julio@diegidio.name> - 2020-08-05 02:36 -0700

csiph-web