jQuery : Form auto Scroll Top on Focus


$('body').on("focus", ':input, select, textarea', function(){
 var center = $(window).height()/2;
 var top = $(this).offset().top ;
 if (top > center){
 var dif = top-center;
 var scroll = (dif > 250 ) ? dif : 250;
 var body = $("html, body");
 body.stop().animate({scrollTop:scroll}, 500, 'swing');
 }
 });

Javascript : Sprintf Function

var sprintf = function(str) {
var args = arguments,
flag = true,
i = 1;
str = str.replace(/%s/g, function() {
var arg = args[i++];
if (typeof arg === ‘undefined’) {
flag = false;
return ”;
}
return arg;
});
return flag ? str : ”;
};

Javascript : Detect Browser

function detectBrowser() {
if(navigator.userAgent.indexOf(“Chrome”) != -1 ){
return “chrome”;
}else if(navigator.userAgent.indexOf(“Opera”) != -1 ){
return “opera”;
}else if(navigator.userAgent.indexOf(“Firefox”) != -1 ){
return “firefox”;
}else if((navigator.userAgent.indexOf(“MSIE”) != -1 ) || (!!document.documentMode == true )){ //IF IE > 10
return “mise”;
}else{
return “unknown”;
}
}

Javascript : Clock Code

function startTime() {
var today=new Date();
var h=today.getHours();
var m=today.getMinutes();
var s=today.getSeconds();
m = checkTime(m);
s = checkTime(s);
document.getElementById(‘time’).innerHTML = h+”:”+m+”:”+s;
var t = setTimeout(function(){startTime()},500);
}

function checkTime(i) {
if (i<10) {i = “0” + i};  // add zero in front of numbers < 10
return i;
}

Javascript : Detect browser using Javascript

var isOpera = !!window.opera || navigator.userAgent.indexOf(‘ OPR/’) >= 0;
// Opera 8.0+ (UA detection to detect Blink/v8-powered Opera)
var isFirefox = typeof InstallTrigger !== ‘undefined’;   // Firefox 1.0+
var isSafari = Object.prototype.toString.call(window.HTMLElement).indexOf(‘Constructor’) > 0;
// At least Safari 3+: “[object HTMLElementConstructor]”
var isChrome = !!window.chrome && !isOpera;              // Chrome 1+
var isIE = /@cc_on!@/false || !!document.documentMode;   // At least IE6

var output = ‘Detecting browsers by ducktyping:<hr>’;
output += ‘isFirefox: ‘ + isFirefox + ‘<br>’;
output += ‘isChrome: ‘ + isChrome + ‘<br>’;
output += ‘isSafari: ‘ + isSafari + ‘<br>’;
output += ‘isOpera: ‘ + isOpera + ‘<br>’;
output += ‘isIE: ‘ + isIE + ‘<br>’;
document.body.innerHTML = output;