Search code examples
jquerycssmailto

Remove an element from Jquery


I have a loader between my pages. The script works when you click on any link. But I don't want this action in a EMAIL LINK (mailto). How Can I avoid to do this script for the mail links ?

var speed = 'speed'; 

$(window).load(function() {
    $(".loader").fadeOut(speed, function() {
        $('a[href], button[href]').click(function(event) {
            var url = $(this).attr('href');
            if (url.indexOf('#') == 0 || url.indexOf('javascript:') == 0) return;
            event.preventDefault();
            $(".loader").fadeIn(speed, function() {
                window.location = url;
            });
        });
    });
}); 

Solution

  • You can use the :not selector or the not() method to exclude elements, but you have to do it on the current selection, as in $('a[href]:not([href^="mailto:"])') or $('a[href], button[href]').not('[href^="mailto:"]'), the version posted by Kristoffer won't work.

    $(function() {
        var speed = 'slow'; 
    
        $(".loader").fadeOut(speed, function() {
            $('a[href]:not([href^="mailto:"])').on('click', function(event) {
                event.preventDefault();
    
                var url = $(this).attr('href');
    
                if (url.indexOf('#') !== 0 && url.indexOf('javascript:') !== 0) {
    
                    $(".loader").fadeIn(speed, function() {
                        window.location = url;
                    });
                }
    
            });
        });
    });