Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I wrote this Jqeury to make it work with asp.net update panel. I know i do little bit of hacking to make it work just. I want to get some help in how to optimize and remove the hacks from my script.

<script type="text/javascript">

    //get all elemnt with class .resource-download
    $.each($(".resource-download"), function () {
        var fileExtension = getFileExtension(this.href);
        if (fileExtension == "" || fileExtension != "pdf") {
            $(this).addClass("hideLinke");
        }
    });

    //load script agine 
    function init() {
        $.each($(".resource-download"), function () {
            var fileExtension = getFileExtension(this.href);
            if (fileExtension == "" || fileExtension != "pdf") {
                $(this).addClass("hideLinke");
            }
        });
    }

    // getFileExtension 
    function getFileExtension(fileURL) {
        if (fileURL.indexOf('.') === -1) { return ""; } //check for no extension
        return fileURL.split('.').pop();
    }

    //loadGridLayout
    function loadGridLayout() {
        $('#grid-wrapper').masonry();
        $('.selectpicker').selectpicker();


        $('.btn-hamburger').click(function () {
            $('.lts-nav').toggleClass('expanded');
            $('.wrapper').toggleClass('wrapper-expanded');
        });



        /* iPhone Site Search Styling */
        var searching = false;

        $('.search-add-on').click(function () {
            if ($(window).width() < 480) {
                $('.section-1 .search-hideable').toggle();
                $('.section-1 .site-search').focus();
                searching = true;
            }
        });

        $('.site-search').blur(function () {
            if ($(window).width() < 480) {
                $('.section-1 .search-hideable').toggle();
                searching = false;
            }
        });

        $(window).resize(function () {
            if ((searching || !$('.site-search').is(':visible')) && $(window).width() > 480) {
                $('.section-1 .search-hideable').toggle(true);
                $('.section-1 .search-hideable').removeAttr('style');
                searching = false;
            }
        });
    }
    //add script to script manager
    var prm = Sys.WebForms.PageRequestManager.getInstance();
    prm.add_endRequest(init);
    prm.add_endRequest(loadGridLayout);

</script>
share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

Some notes in no particular order:

1

//get all elemnt with class .resource-download
$.each($(".resource-download"), function () {
    var fileExtension = getFileExtension(this.href);
    if (fileExtension == "" || fileExtension != "pdf") {
        $(this).addClass("hideLinke");
    }
});

//load script agine 
function init() {
    $.each($(".resource-download"), function () {
        var fileExtension = getFileExtension(this.href);
        if (fileExtension == "" || fileExtension != "pdf") {
            $(this).addClass("hideLinke");
        }
    });
}

better to define the function and call it rather than copy paste the code.

function init() {
    $.each($(".resource-download"), function () {
        var fileExtension = getFileExtension(this.href);
        // see point 2
        if (fileExtension == "" || fileExtension != "pdf") {
            $(this).addClass("hideLinke");
        }
    });
}

init();

2

if (fileExtension == "" || fileExtension != "pdf") {

Is pointless, "" !== "pdf" so you should just have the second condition.

if (fileExtension !== "pdf") {

3
It's better to be in the habit of using === and !== to avoid subtle bugs caused by type coercion.

4

$.each($(".resource-download"), function () {}); is better written as $(".resource-download").each(function() {});

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.