If I write something like this:
var img = $(new Image()).attr('src', image.src);
How can I check later if img var is an image and not something else ?
If I write something like this:
var img = $(new Image()).attr('src', image.src);
How can I check later if img var is an image and not something else ?
Share Improve this question asked Sep 2, 2009 at 12:31 vsyncvsync 131k59 gold badges340 silver badges423 bronze badges3 Answers
Reset to default 6 if ( img.is('img') ){
}
for safety I may be tempted to wrap the var in jQuery again just incase you may have changed the img to a dom node or something else...
if ( $(img).is('img') ){
}
img.filter('img')
If this returns something then it is an image.
You should avoid explicit type checking.
Use polymorphism to select what has to be done with images, and what has to be done with other objects.
var img = $(new Image())(...);
img.process = function(){ ... do whatever images need ... };
objs.push( img );
var txt = new Text();
txt.process = function(){ .. do text processing, spellcheck, ... };
objs.push( txt );
...
objs.each( o ) { o.process(); }