2

I tried to check in my custom item display template if a picture exists by url. Sometimes the site Logo is set to some path of an picture, but the picture doesn't exist anymore. I would like to replace it with a default picture.

UPDATE!

I try already code below but got an error:

SCRIPT7002: XMLHttpRequest: Network Error 0x80070005, Access is denied.

The url of the picture (http://myCompany.com/sites/jantje/PublishingImages/right_folder_arrow_move_archive.png) is inside the current website, but is not existed because it is already removed.

    var noPictureFoundImgUrl = "/_layouts/15/Images/MyCompany.Intranet/default.png"

    var imageUrl = null;

    if ($isEmptyString(ctx.CurrentItem.SiteLogo))
    {
        imageUrl = noPictureFoundImgUrl;
    }
    else
    {
        var http = new XMLHttpRequest();
        http.open('HEAD', ctx.CurrentItem.SiteLogo, false);
        http.send();

        // check if sitelogo exists
        if(http.status != 404)
        {
            imageUrl = ctx.CurrentItem.SiteLogo;
        }
        else
        {
            imageUrl = noPictureFoundImgUrl;
        }
    }
  • How about using $.get jquery function api.jquery.com/jquery.get . You can check whether the request failed or succeeded and run your logic accordingly – HarryB Nov 18 '15 at 12:15
  • I got an access denied error. – Ola Nov 18 '15 at 13:36
  • check my answer for an alternative approach. – HarryB Nov 18 '15 at 14:54
  • which answer do you mean? – Ola Nov 19 '15 at 8:15
1

You can use XMLHttpRequest() to check if image exists.

function imageExists(image_url){

    var http = new XMLHttpRequest();
    http.open('HEAD', image_url, false);
    http.send();
    return http.status != 404;

}

So in your display template, you can update it as below

// check if sitelogo exists
if(imageExists(pictureUrl))
{
    imageUrl = pictureUrl;
}
else
{
    imageUrl = noPictureFoundImgUrl;
}

Also you can use jQuery $.get.

$.get(pictureUrl)
.done(function() { 
    // Do something now you know the image exists.
    imageUrl = pictureUrl;

}).fail(function() { 
    // Image doesn't exist - do something else.
    imageUrl = noPictureFoundImgUrl;
})

Refer Check if image exists on server using JavaScript?

0

Try using onError event of the img tag, one example:

<img src="expectedimage.gif" alt="Expected Image not found" onError="this.src='defaultImage.gif';" />

This topic has been discussed in detail here and here.

  • I dont using <img> but I use a div with a background image. I try the onError solution on a div, but it didnt work. <div class="cbs-pictureOnTopImageContainer" style="background-image: url(#= imageUrl =#);" id="#= pictureContainerId =#" onError="this.src = '#= noPictureFoundImgUrl =#';"></div> – Ola Nov 19 '15 at 8:14
  • div doesnt have an onerror event. In this case you can try specifying your noPictureFoundImgUrl in the backgroung image property such as: background-image: url('expectedimageurl'), url('noPictureFoundImgUrl'); – HarryB Nov 21 '15 at 20:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.