OL Learn

X in Dynamic Image when Image not found


I have a folder which contains a number of images. I pull the images as a dynamic image. If the image name isn’t present in the folder, I get a large X. I’d prefer it to be blank if the image file isn’t found. One caveat is that the full image page will be present, so I can’t check if the data variable or path is empty or NULL. Below is my script that I’d essentially like to work. I’m not sure if how to assess the value of the result in a dymanic image.

var path, newPath, result = “”;

var countryID = record.fields[“childid_ne”].substring(0,3);

path = “Somepath” + countryID + “_FLAG.jpg”;
newPath = path.replace(/\/g,"/");

if (path !== “”) {
result += “file:///” + newPath;

if (result !== “”)
results.attr(“src”, result);
else results.hide();

You can use resource() to determine if the file is there, then apply your logic based on the result.


var image_path = "file:///c:/TESTS/";
var image_name = "myImage.jpg";

imageCheck(encodeURI(image_path + image_name));

function imageCheck(val){
	var rVal = resource(val);
	if(rVal != "" && rVal != null){
		return val;

That script check for image exist and prevent the red x in case of image does not exist…

Hi @TSled,

Please use the following JavaScript code:

if (result !== "" && resource(result)) {
    results.attr("src", result);
} else {

Instead of:

if (result !== "")
results.attr("src", result);
else results.hide();

Edited: Typo, following @Sharne 's tip.

Typo && resource(result)

1 Like

That’s perfect…works great.
Thanks everyone.

1 Like