1

I want to use array instead of using

if(imageExtension == 'jpg' || imageExtension == 'png' || imageExtension == 'gif' ). C

Can someone please give me a clue :D

setupChangeImage : function() {
        $('.carousel li a').each(function(){
            $(this).click(function(e){
                e.preventDefault();
                var src = $(this).attr('href');
                var dataSource = $(this).data('src');
                var imageExtension = dataSource.substr(dataSource.length-3, 3);             
                if (src != $('.gallery img.gallery-main-image,.main-image .main-image-first').attr('src').replace(/\?(.*)/,'')){
                    $('.gallery img.gallery-main-image,.main-image .main-image-first,.main-image-last').stop().animate({
                        opacity: '0'
                    }, function(){
                        $('.loading').show();
                        $(this).attr('src', src);
                        $('.main-image-second').remove();
                        if(imageExtension == 'jpg' || imageExtension == 'png' || imageExtension == 'gif' ){
                            var imageLast = '<div class="main-image-second"><img class="main-image-last" src="'+ dataSource +'" alt="" /></div>';
                            $('.main-image').append(imageLast);
                        }
                        else {
                            var iframe = '<div class="main-image-second"><iframe class="main-image-last" width="657" height="432" src="'+ dataSource +'" style="border:none;margin-top:14px;" scrolling="no" marginwidth="0"></iframe></div>';
                            $('.main-image').append(iframe);
                        }
                    }).load(function(){
                        $('.loading').hide();
                        $(this).stop().animate({
                            opacity: '1'
                                                                                       });                      
                });
                }           
            });
        });
        }
3
  • /^(jpg|png|gif|)$/.test(imageextension) if you are interested in regex. Commented Aug 30, 2013 at 5:27
  • jquery has inArray - api.jquery.com/jQuery.inArray Commented Aug 30, 2013 at 5:28
  • It's well worth spending an hour reading through the jQuery API. That's all the time it takes, and it pays you back massively. Commented Aug 30, 2013 at 5:29

2 Answers 2

3
var arr = ["jpg", "png","gif" ];
if(jQuery.inArray(imageExtension, arr) !== -1){
//found 
}else{
//not found 
}

refer http://api.jquery.com/jQuery.inArray/

Sign up to request clarification or add additional context in comments.

Comments

-1

This will work without jQuery.

if (['jpg', 'png', 'gif'].indexOf(imageExtension) > -1) ...

Run this somewhere on the page to make it work in IE8:

if (!Array.prototype.indexOf) {
    Array.prototype.indexOf = function indexOf(search) {
        for (var i = 0; i < this.length; i++) {
            if (this[i] === search) return i;
        }
        return -1;
    };
}

You might as well use jQuery I guess.

1 Comment

Note that IE8, which is a very popular browser (about 23% of global browser users use it), doesn't support Array#indexOf. Since the OP is using jQuery, jQuery's inArray (as suggested by SAM and, briefly, myself) is the better choice.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.