Few useful js / jquery functions in day today life


/*Element auto scroller*/
function scroll_to_bottom(obj,speed){
    //$('html, body').animate({scrollTop: $('html, body')[0].scrollHeight});
    
    if(!obj){
        obj = $('html, body');
    }
    if(!speed){
        speed = '';
    }
    
    obj.animate({scrollTop: obj[0].scrollHeight},speed);
}
function scroll_to_top(obj,speed,top_margin){
    //$('html, body').animate({scrollTop:0});
    if(!obj){
        obj = $('html, body');
    }
    if(!speed){
        speed = '';
    }
    if(!top_margin){
        top_margin = 0;
    }
    obj.animate({scrollTop:top_margin},speed);
}
usage
$(document).ready(function(){
   
var obj = $('.class_of_element');
Or
var obj = $('#id_of_element');
var speed = 2000; //in milliseconds ie 2 second 
scroll_to_bottom(obj,speed)

});
/*get base url*/
var protocol = window.location.protocol;
var hostname = window.location.hostname;
var port = window.document.location.port;
var full_pathname = window.location.pathname;
var split_pathname = full_pathname.split( '/' );
var pathname = split_pathname[1];

/*Server path*/
//var base_url = protocol+"//"+hostname;
/*Server path*/

/*Local path*/
var base_url = protocol+"//"+hostname+"/"+pathname;
/*Local path*/
//Selected == selected if you know option value
var c_id = 3; //option value

 $("#selectbox_id option").each(function(){
        if($(this).val() == c_id){
            $(this).attr("selected","selected");
            return false;
        }
    });

OR


 $('#selectbox_id>option:eq('+u_id+')').attr('selected', true);

/*Returns '-------' if variable passed is null, blank, or undefined.*/
    function is_blank(val){
        if(!val){
            var str = '-------';
        }
        else{
            var str = val;
        }
        
        return str;
    }
    /*Returns '-------' if variable passed is null, blank, or undefined.*/

//skip script tag in text area // js malware
function strip_script_tag_js(fieldStr){
    //code to replace  
    var new_fieldStr=fieldStr.replace(/(\|\<\/script\>)/gi,"");
    return new_fieldStr;
}

// Check minimum characters = 6
    if($(this).hasClass('min-6') == true){
        var characterReg = /^([a-zA-Z0-9]{0,6})$/;
        if(characterReg.test(inputVal)){
            $parentTag.addClass('error').append($error.clone().text('Minimum 6 characters'));
        }
    }
$('textarea[maxlength]').keyup(function(){  
        //get the limit from maxlength attribute  
        var limit = parseInt($(this).attr('maxlength'));  
        //get the current text inside the textarea  
        var text = $(this).val();  
        //count the number of characters in the text  
        var chars = text.length;  
  
        //check if there are more characters then allowed  
        if(chars > limit){  
            //and if there are use substr to get the text before the limit  
            var new_text = text.substr(0, limit);  
  
            //and change the current text with the new text  
            $(this).val(new_text);  
        }  
    });  

Share on Facebook




About Pragnesh Karia

Pragnesh Karia, Open Source Enthusiast, Software Professional, Software Developer, Technical Lead ,Magento, Joomla ,Joomla LMS , Moodel LMS ,PHP ,Mysql, Ajax, Javascript, Jquery, Linux, Fan of Open Sources , Annet Technologies , SEO Analyst , Mootools