I've actually ran out of ideas. I probably have sloppy code here, but it works, until it's missing an array which is returning undefined
. I've tried the following:
if( typeof myVar == "undefined"){}
if(myVar === "undefined"){}
if(myVar != undefined){}
-- basically, most things to my knowledge.
Now, I know how to catch an undefined or null object normally, but this is frustrating me.
When one of the arrays returns undefined, it's saying:
GET http://127.0.0.1:2368/undefined/ 404 (Not Found)
Here's the JS:
var postImage = $(".post-image");
var imageURL =
$('.post-image img').map(function () {
return this.src;
});
var i = 0;
for (var i = 0; i < imageURL.length; i++) {
postImage.each(function () {
$(this).append('<div class="fetch-image" style="background-image:url(' + imageURL[i++] + ')">');
$(".post-image img").remove();
});
}
So the question is...
I want to be able to replace the /undefined/
URL with a fallback backgroundImage
, in case no image was set in the first place. The image src
is only collected if an image is there (obviously), but in case there is no image to get the src
, I want to be able to set a fallback, that replaces the missing or undefined
array.
Hope this makes sense!
Many regards,
James.
EDIT: I've tried the following, inside and outside the each()
scope:
if (typeof imageURL === "undefined") {
alert("something is undefined");
}
I must be missing something here.