-6

Incase a img is not found , i am showing an alternate default image this way

var defaultimage = "images/emptyimage.jpg";


divhtml.append('<i><img class = "imgclss" id="'+name+'" src="'+imagename+'" onerror="this.onerror=null;this.src='+defaultimage+'"/></i>');

But in browser console , i am getting this undefined error

Uncaught ReferenceError: emptyimage is not defined

could anybody please help me how to resolve this errors?

I was folowing this link to resolve the issue

how to show alternate image if source image is not found? (onerror working in IE but not in mozilla)

Community
  • 1
  • 1
Pawan
  • 31,545
  • 102
  • 256
  • 434

3 Answers3

3

Your error is here :

onerror="this.onerror=null;this.src='+defaultimage+'"

After the interpretation, it look like this :

onerror="this.onerror=null;this.src=images/emptyimage.jpg"

You need to wrap your variable to make it a string once the javascript interpreted :

onerror="this.onerror=null;this.src=\''+defaultimage+'\'"
Karl-André Gagnon
  • 33,662
  • 5
  • 50
  • 75
-1

Try this:

var defaultimage = new Image();
defaultimage.src = "images/emptyimage.jpg";

// define `name` and `imagename`

divhtml.append('<i><img class="imgclss" id="'+name+'" src="'+imagename+'" onerror="this.onerror=null;this.src='+defaultimage.src+'"/></i>');
web-nomad
  • 6,003
  • 3
  • 34
  • 49
-1

How do I fix uncaught ReferenceError is not defined?

Answer: Execute Code after jQuery Library has Loaded

The most common reason behind the error "Uncaught ReferenceError: $ is not defined" is executing the jQuery code before the jQuery library file has loaded. Therefore make sure that you're executing the jQuery code only after jQuery library file has finished loading.