I have written a jquery code for a pop up but the problem is I wanted to optimize the jquery code can you tell me how to optimise it to have a better results in the browser..

it would be great if u tell for html and css too....

I am providing jquery code below....

http://jsfiddle.net/00831/SSMX4/17/

// locale selector actions
$('#region-picker').click(function(){
    if ($("#locale-select").is(":visible")) return closeSelector('slide');
    var foot_height = $('#footer').innerHeight();
    var foot_height_css = foot_height-1;
    var select_position = '-=' + (Number(700)+18);
    console.log("hallo"+select_position);
    var $selector = $('#locale-select');
    $('#locale_pop').fadeOut();
    $selector.css({top:foot_height_css});
    $selector.fadeIn(function(){
        $(this).addClass('open');
        $(this).animate({top:select_position}, 1000);
        });
});
$('#select-tab').click(function(e){
    e.stopPropagation()
    closeSelector('slide');
    });
// don't hide when clicked within the box
$('#locale-select').click(function(e){
    e.stopPropagation();
});
$(document).click(function(){ 
    if ($('#locale-select').hasClass('open')) {
        closeSelector('disappear');
    }
});

$('.locale-link').click(function(){
    //var $clicked = $(this); //"$(this)" and "this" is the clicked span
    $(".locale-select-lable").html($(this).html());
    //search for "marginXXXXX"
    var flags = $(this).attr("class").match(/(margin\w+)\s/g);
    //create new class; add matching value if found
    var flagClass = "tk-museo-sans locale-select-lable" + (flags.length ? " " + flags[0] : "");
    //set new class definition
    $(".locale-select-lable").attr("class", flagClass);

    closeSelector('disappear');

    //if ($("#locale-select").is(":visible")) return closeSelector('slide');
   /*         
   // var desired_locale = $(this).attr('rel');
   // createCookie('desired-locale',desired_locale,360);
   // createCookie('buy_flow_locale',desired_locale,360);
    //closeSelector('disappear');
    */
});  /* CORRECTED */

$('#locale_pop a.close').click(function(){
    var show_blip_count = readCookie('show_blip_count');
    if (!show_blip_count) {
        createCookie('show_blip_count',3,360);
    }
    else if (show_blip_count < 3 ) {
        eraseCookie('show_blip_count');
        createCookie('show_blip_count',3,360);
    }
    $('#locale_pop').slideUp();
    return false;
});
share|improve this question

1 Answer

Here's a breakdown of what I did to your code to optimize it. I din't test it though since nothing really happens on jsFiddle. But comments on the code will help:

//cache commonly used elements
//or example, the following calls jQuery to make them into jQuery objects
//you previously did this on every event handler
var localeSelect = $("#locale-select"),
    localePop = $('#locale_pop'),
    footer = $('#footer'),
    localeSelectLabel = $(".locale-select-lable");

//follow the natural progression of parsing, which would be:
//- variable declaration
//- function declaration
//- operations
//for more info, research on the difference of 
//function declarations and function expressions and how they are parsed
function closeSelector(hide_type) {

    //personally, I go for the comma separated, single var keyword approach
    //it emphasizes the variable block
    //but it's up to you. 
    var foot_height = footer.innerHeight(),
        select_position = '+=' + (Number(400) + 20);

    if (hide_type == 'slide') {
        localeSelect.animate({
            top: select_position
        }, 1000, function() {

            //according to jQuery, the value of "this" is the DOM element animated
            //but you already have reference to localeSelect, a jQuery object
            //wrapping "this" in jQuery is redundant. use the reference instead
            //saves you one function call
            localeSelect
                .removeClass('open')
                .fadeOut();
        });
    } else if (hide_type == 'disappear') {
        localeSelect
            .fadeOut('fast')
            .removeClass('open');
    }
}

$('#region-picker').click(function() {

    var foot_height = footer.innerHeight(),
        foot_height_css = foot_height - 1,
        select_position = '-=' + (Number(700) + 18);

    //although allowable, but it's better you use braces for code blocks        
    if (localeSelect.is(":visible")){
        return closeSelector('slide');    
    }

    localePop.fadeOut();

    //chainability is a good asset of jQuery
    //so as long as the previous call returns the same object
    //you can chain a function that operates that object
    localeSelect
        .css({
            top: foot_height_css
        })
        .fadeIn(function() {
            $(this)
                .addClass('open')
                .animate({
                    top: select_position
                }, 1000);
        });
});

$('#select-tab').click(function(e) {
    e.stopPropagation();
    closeSelector('slide');
});

localeSelect.click(function(e) {
    e.stopPropagation();
});
$(document).click(function() {
    if (localeSelect.hasClass('open')) {
        closeSelector('disappear');
    }
});

$('.locale-link').click(function() {
    var flags = $(this).attr("class").match(/(margin\w+)\s/g),
        flagClass = "tk-museo-sans locale-select-lable" + (flags.length ? " " + flags[0] : "");


    localeSelectLabel
        .html($(this).html())
        //just wondering, is this an "erase all classes and replace with" operation?  
        //jQuery has a convenient .addClass(className) function        
        .attr("class", flagClass); 

    closeSelector('disappear');

});

$('#locale_pop a.close').click(function() {

    //i see the name of the cookie redundantly
    //why not store it in one location and use that variable?
    var cookieName = 'show_blip_count',
        show_blip_count = readCookie(cookieName);

    if (show_blip_count < 3) {
        eraseCookie(cookieName);
    }

    //in both operations, you create a cookie, why not factor it out?
    createCookie(cookieName, 3, 360);

    localePop.slideUp();
    return false;
});


​
share|improve this answer

Your Answer

 
or
required, but never shown
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.