RGBK
RGBK

Reputation: 2048

jQuery Error - Cannot call method 'indexOf' of undefined

I have this code to fix the issue of flash hopping over all your content in the z-index:

$("iframe").each(function(){
    var ifr_source = $(this).attr('src');
    var wmode = "wmode=opaque";
    if(ifr_source.indexOf('?') != -1) {
        var getQString = ifr_source.split('?');
        var oldString = getQString[1];
        var newString = getQString[0];
        $(this).attr('src',newString+'?'+wmode+'&'+oldString);
    }
    else $(this).attr('src',ifr_source+'?'+wmode);
});

For some reason, i'm getting errors on the indexOf, and it's breaking the site. But oddly, it's not breaking it entirely, just one css resize fix I have on there.

Uncaught TypeError: Cannot call method 'indexOf' of undefined

The site is here: http://syndex.me

Would love to know why this is happening.

Upvotes: 1

Views: 9716

Answers (2)

mAu
mAu

Reputation: 2018

Get the src directly from this. You do not need to create a whole jQuery object for this.

ifr_source = this.src;

As a bonus, this.src returns an empty string even if the attribute src is not set, whereas jQuery's attr returns undefined and you have to perform a check before using indexOf.

Upvotes: 2

Shadow Wizard
Shadow Wizard

Reputation: 66389

Probably you have iframe without any src attribute declared at all.

Have this to avoid the error:

var ifr_source = $(this).attr('src') || "";

Edit: viewing the source here it is indeed:

<iframe id="likeit"></iframe>

On second thought, adding to the URL of this frame won't be good idea, better just leave it alone, so final code should be:

var ifr_source = $(this).attr('src') || "";
if (ifr_source.length > 0) {
    var wmode = "wmode=opaque";
    if(ifr_source.indexOf('?') != -1) {
        var getQString = ifr_source.split('?');
        var oldString = getQString[1];
        var newString = getQString[0];
        $(this).attr('src',newString+'?'+wmode+'&'+oldString);
    }
    else
        $(this).attr('src',ifr_source+'?'+wmode);
}

Upvotes: 1

Related Questions