开发者

How do I check if prettyPhoto is defined

I have this snippet that is on my main js file that our entire site uses

$("a[rel^='prettyPhoto']").prettyPhoto({
        theme: 'light_square',
        showTitle: false,
        allow_resize: true
    });

that 开发者_运维技巧problem is that on some pages prettyPhoto is undefined and causes an error in firebug so i thought i would try this

if(typeof prettyPhoto=="undefined"){
    //do nothing
}else{
    $("a[rel^='prettyPhoto']").prettyPhoto({
        theme: 'light_square',
        showTitle: false,
        allow_resize: true
    });
}

but this always executes to true, even on pages that prettyPhoto is available....any ideas


Try this:

if (typeof $.fn.prettyPhoto == "function") {
    // we have prettyPhone on the page
}


if you do a console.log(prettyPhoto) on a page that you know has prettyPhoto, what does it say it is? an object?

If so, then you do

if(typeof prettyPhoto === 'object'){
    //do your stuff here.
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜