0

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.

  • Why not generate broken url at first place? – zerkms Jan 26 '14 at 21:37
  • possible duplicate of [Detecting an undefined object property in JavaScript](http://stackoverflow.com/questions/27509/detecting-an-undefined-object-property-in-javascript) – scrowler Jan 26 '14 at 21:38

2 Answers2

3

All of the things you tried are wrong...

if( typeof someVariable == "undefined")
Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • Sorry, I'll update my post. I have attempted this, typo errors! Thank you. – James Martin-Davies Jan 26 '14 at 21:38
  • That's not true. He did compare to `undefined` which should work. The only advantage `typeof` has is that it does not throw an error if the variable hasn't been defined. But I think that's a dubious advantage. – Wayne Jan 26 '14 at 21:45
  • @lwburk That wasn't in the question before it was edited ;) Only a typoed `typeOf` and two different comparisons against the string `"undefined"`. – Niet the Dark Absol Jan 26 '14 at 21:46
1

Your loop includes:

imageURL[i++] 

This will increment the value of i twice with each iteration. Thus, the loop will continue even though the index on the last iteration will out of the array's bounds.

Your array sub index should just be set to just i (imageURL[i]), and set the third for parameter to i+=2. The loop should then not return undefined values (since i will always be within the bounds of the array).

Lil' Bits
  • 898
  • 2
  • 9
  • 24
  • I *think* this is the right answer, but it's not a very good question. Anyway, +1 because this smells dirty at the least. – Wayne Jan 26 '14 at 21:51
  • Although I had to sit and think about this a little after I submitted the answer. The bounds of the array will still all be valid, because he's using a post-increment operator within the index. His loop structure should still work (it will just skip some indexes). But I am still leaning toward fixing the loop and/or array structure instead of throwing in a band-aid for `undefined`. – Lil' Bits Jan 26 '14 at 21:51
  • It's still not known to me why he's doubly incrementing either. – Lil' Bits Jan 26 '14 at 21:54
  • It's just a poorly asked question. Probably not worth thinking too much about, unfortunately. – Wayne Jan 26 '14 at 21:57
  • It's the questions that *seem* the easiest that rattle around in your brain the most unfortunately :) – Lil' Bits Jan 26 '14 at 21:59