Showing posts with label Javascript. Show all posts
Showing posts with label Javascript. Show all posts

Friday, September 2, 2011

Load jQuery Asyc

This is a good javascript plugin that loads jQuery async and runs document.ready after jQuery is loaded and DOM initilized.

http://www.yterium.net/jQl-an-asynchronous-jQuery-Loader

Friday, August 19, 2011

Oops in Javascript - Part -1

In this post i will show you how to create Classes and object instance in Javascript.

var  myclass = function  (arg) {
console.log("Class initiated" );//Check object is initiated or not 
this .arg = arg;
}
myclass.prototype.myfunc = function  () {
console.log(this .arg);
}
function  TestOops() {
var  c = new  myclass("obj1" ); //create first instance for myclass
c.myfunc();//prints obj1 
var  d = new  myclass("obj2" );//create second instance for myclass
d.myfunc(); //prints obj2 
c.myfunc(); //prints obj1 
}

How to create a callback function in Javascript


function  MyCallback(name, callbakFn) {        
if  (typeof  (callbakFn) == 'function' ) {
callbakFn.call(this , name);
}
}
function  TestCallback() {
MyCallback('sriram' , function  (arg) {
alert(arg);
});
}


Wednesday, August 10, 2011

toTitleCase jQuery Plugin


(function  ($) {
$.fn.toTitleCase = function  () {
$(this ).each(function  () {
var  headline = $(this ).text();
$(this'\uc1\u8216?\uc1\u8217?"\uc1\u8220?.@:\\/\\{\\(\\[<>_]+-? *)/g, function  (match, pl, index, title) {
if  (index > 0 && title.charAt(index - 2) !== ":"  && match.search(/^(a(nd?|s|t)?|b(ut|y)|en|for|i[fn]|o[fnr]|t(he|o)|vs?\\.?|via)[ \\-]/i) > -1)
return  match.toLowerCase();
if  (title.substring(index - 1, index +'"_{(\\[]/) > -1)
return  match.charAt(0) + match.charAt(1).toUpperCase() + match.substr(2);
if  (match.substr(1).search(/[A-Z]+|&|[\\w]+[._][\\w]+/) > -1 || title.substring(index - 1, index + 1).search(/[\\])}]/) > -1)
return  match;
return  match.charAt(0).toUpperCase() + match.substr(1);
}));
});
};
})(jQuery);
Source: http://plugins.jquery.com/project/titlecase

Title Case Javascript Extension


String.prototype.toTitleCase = function  () {
return  this'\uc1\u8216?\uc1\u8217?"\uc1\u8220?.@:\\/\\{\\(\\[<>_]+-? *)/g, function  (match, p1, index, title) {
if  (index > 0 && title.charAt(index - 2) !== ":"  &&
	match.search(/^(a(nd?|s|t)?|b(ut|y)|en|for|i[fn]|o[fnr]|t(he|o)|vs?\\.?|via)[ \\-]/i) > -1)
return  match.toLowerCase();
if  (title.substring(index - 1, index +'"_{(\\[]/) > -1)
return  match.charAt(0) + match.charAt(1).toUpperCase() + match.substr(2);
if  (match.substr(1).search(/[A-Z]+|&|[\\w]+[._][\\w]+/) > -1 ||
	title.substring(index - 1, index + 1).search(/[\\])}]/) > -1)
return  match;
return  match.charAt(0).toUpperCase() + match.substr(1);
});
};

Source: http://individed.com/code/to-title-case/

Thursday, July 7, 2011

Get QueryString value using javascript

function  getAllQueryString() {
var  vars = [], hash;
var  hashes = window.location.href.slice(window.location.href.indexOf( ) + 1).split( );
for  (var  i = 0; i < hashes.length; i++) {
hash = hashes[i].split( );
vars.push(hash[0]);
vars[hash[0]] = hash[1];
}
return  vars;
}
function  getQueryString(name) {
return  getUrlVars()[name];
}


Get the QueryString using the following Syntax


var  hID = getQueryString( );

Tuesday, July 5, 2011

How to detect popup blocker in Javascript


var  sw1 = window.open(urlPath,"windowname"  , settings);
if  (sw1)
sw1.focus();
else 
alert("Popup blocker detected" );

Friday, July 1, 2011

Validate Email - No under score or @ after first @


<script  type="text/javascript"> 
function  fn_Email_Check(Chartocheck) {
var  rx = /^([A-Za-z0-9_\\-\\.])+\\@([A-Za-z0-9\\-\\.]+[^_@])+\\.([A-Za-z]{2,4})$/;
if  (rx.test(Chartocheck) == false )
return  false ;
else 
return  true ;
}
</script> 

Infinite Loop Javascript timer using Recursive function


<html> 
<head> 
<script  type="text/javascript"> 
var  c = 0;
var  t;
var  timer_is_on = 0;
function  timedCount() {
document.getElementById( ).value = c;
c = c + 1;
t = setTimeout("timedCount()" , 1000);
}
function  doTimer() {
if  (!timer_is_on) {
timer_is_on = 1;
timedCount();
}
}
</script> 
</head> 
<body> 
<form> 
<input  type="button"  value="Start count!"  onclick="doTimer()"> 
<input  type="text"  id="txt"  /> 
</form> 
</body> 
</html>

Wednesday, June 15, 2011

Validate Email - Javascript


<script  type="text/javascript"> 
//Function to validate Email 
function  IsValidEmail(email) {
//var exclude=/[^@\\-\\.\\w]|^[_@\\.\\-]|[\\._\\-]{2}|[@\\.]{2}|(@)[^@]*\\1/; 
var  exclude = /[^@\\-\\.\\w]|^[_@\\.\\]|[\\._\\-]{2}|[@\\.]{2}|(@)[^@]*\\1/;
var  checkend = /\\.[a-zA-Z]{2,3}$/;
if  ((email.search(exclude) != -1) || (email.search(checkend) == -1)) {
return  false ;
}
atPos = email.indexOf("@" , 0);
pPos1 = email.indexOf("." , 0);
periodPos = email.indexOf("." , atPos);
pos1 = pPos1;
pos2 = 0;
while  (pos2 > -1) {
pos2 = email.indexOf("." , pos1 + 1);
if  (pos2 == pos1 + 1) {
return  false ;
} else  {
pos1 = pos2;
}
}
if  (atPos == -1) {
return  false ;
}
if  (atPos == 0) {
return  false ;
}
if  (pPos1 == 0) {
return  false ;
}
if  (email.indexOf("@" , atPos + 1) > -1) {
return  false ;
}
if  (periodPos == -1) {
return  false ;
}
if  (atPos + 1 == periodPos) {
return  false ;
}
if  (periodPos + 3 > email.length) {
return  false ;
}
return  true ;
}
</script>

Trim String Using Javascript

Javascript Trim Member Functions

Use the code below to make trim a method of all Strings. These are useful to place in a global Javascript file included by all your pages.

String.prototype.trim = function() {
 return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
 return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
 return this.replace(/\s+$/,"");
}
 

Javascript Trim Stand-Alone Functions

If you prefer not to modify the string prototype, then you can use the stand-alone functions below.
function trim(stringToTrim) { return stringToTrim.replace(/^\s+|\s+$/g,""); } function ltrim(stringToTrim) { return stringToTrim.replace(/^\s+/,""); } function rtrim(stringToTrim) { return stringToTrim.replace(/\s+$/,""); }

Compatibility

The functions above use regular expressions, which are compatible with Javascript 1.2+ or JScript 3.0+. All modern browsers will support this. If you require functions for older versions of Javascript back to version 1.0, try the functions below adapted from the Javascript FAQ 4.16. These strip the following, standard whitespace characters: space, tab, line feed, carriage return, and form feed. The IsWhitespace function checks if a character is whitespace.

function ltrim(str) { 
 for(var k = 0; k < str.length && isWhitespace(str.charAt(k)); k++);
 return str.substring(k, str.length);
}
function rtrim(str) {
 for(var j=str.length-1; j>=0 && isWhitespace(str.charAt(j)) ; j--) ;
 return str.substring(0,j+1);
}
function trim(str) {
 return ltrim(rtrim(str));
}
function isWhitespace(charToCheck) {
 var whitespaceChars = " \t\n\r\f";
 return (whitespaceChars.indexOf(charToCheck) != -1);
}