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

Tuesday, 24 September 2013

Javascript anonymous functions

Anonymous functions are functions that are dynamically declared at runtime. They’re called anonymous functions because they aren’t given a name in the same way as normal functions.
Anonymous functions are declared using the function operator instead of the function declaration. You can use the function operator to create a new function wherever it’s valid to put an expression. For example you could declare a new function as a parameter to a function call or to assign a property of another object.
Here’s a typical example of a named function:
1.  function flyToTheMoon()
2.  {
3.    alert("Zoom! Zoom! Zoom!");
4.  }
5.  flyToTheMoon();
Here’s the same example created as an anonymous function:
1.  var flyToTheMoon = function()
2.  {
3.    alert("Zoom! Zoom! Zoom!");
4.  }
5.  flyToTheMoon();

For more :  http://helephant.com/2008/08/23/javascript-anonymous-functions/

Friday, 3 May 2013

Differences between Get Vs Post


GET vs POST

·         A GET request is used to get data from the server.
·         A POST request is used for modifying data on the server.

When to use GET

If the processing of a form is idempotent (i.e. it has no lasting observable effect on the state of the world), then the form method should be GET. Many database searches have no visible side-effects and make ideal applications of query forms.

Characteristics of GET:

·         Use GET for safe actions and POST for unsafe actions.
·         GET requests can be cached
·         GET requests can remain in the browser history
·         GET requests can be bookmarked
·         GET requests can be distributed & shared
·         GET requests can be hacked

When to use POST

If the service associated with the processing of a form has side effects (for example, modification of a database or subscription to a service), the method should be POST.
Use POST when dealing with long requests – if you’re sending large amounts of data, or sensitive data over HTTPS, you will want to use POST. Some browser such as Internet Explorer place a limit on the URL string so this may break the action of some forms if you use GET.

You may consider using POST for the following actions:

·         Posting a message to a bulletin board, newsgroup, mailing list, or similar group of articles
·         Providing a block of data, such as the result of submitting a form, to a data-handling process
·         Extending a database through an append operation
·         Annotation of existing resources

Good Luck…

Thursday, 14 March 2013

Using Slice(), Substring(), And Substr() In Javascript


String methods

In the book, Eloquent Javascript: A Modern Introduction to Programming, Marijn Haverbeke takes the reader through a good number of string-parsing examples. And, in his examples, Haverbeke makes frequent use of the String method, slice(). When I think of slice(), I think of extracting portions of a Javascript array; I don't think I was even aware that the Javascript String prototype had a slice() method. To clear up my own confusion and misconceptions, I wanted to take a quick look at the various ways in which Javascript allows for partial string extraction.
From what I can see, there are three primary methods for substring extraction:
  • String.slice( begin [, end ] )
  • String.substring( from [, to ] )
  • String.substr( start [, length ] )
In all cases, the second argument is optional. If it is not provided, the substring will consist of the start index all the way through the end of the string. For both the slice() and substring() methods, the second argument is exclusive; that is, the resultant substring will not contain the character at the final index.
Let's take a look as these three approaches in action:
  • <!DOCTYPE html>
  • <html>
  • <head>
  • <title>Extracting Substrings In Javascript</title>
  •  
  • <script type="text/javascript">
  •  
  •  
  • // For these demos, let's create a numbered string so that
  • // we can easily see where the indexes come into play with
  • // all of the substring methods.
  • //
  • // NOTE: We are starting at zero since Javascript is all
  • // zero-based.
  • var numbers = "0123456789";
  •  
  •  
  • // -------------------------------------------------- //
  • // -------------------------------------------------- //
  •  
  •  
  • // String.slice( begin, end )
  •  
  • // Let's start by using both begin and end.
  • console.log(
  • "slice( 3, 7 ) :",
  • numbers.slice( 3, 7 )
  • );
  •  
  • // What happens when we start with a negative number.
  • console.log(
  • "slice( -7, 7 ) :",
  • numbers.slice( -7, 7 )
  • );
  •  
  • // What happens when we use two negative numbers.
  • console.log(
  • "slice( -7, -3 ) :",
  • numbers.slice( -7, -3 )
  • );
  •  
  • // What happens when we omit the last argument.
  • console.log(
  • "slice( 3 ) :",
  • numbers.slice( 3 )
  • );
  •  
  • // And with the negative, end-relevant index.
  • console.log(
  • "slice( -7 ) :",
  • numbers.slice( -7 )
  • );
  •  
  • // If the index is out of range, it returns the empty string.
  • console.log(
  • "slice( 100, 101 ) :",
  • numbers.slice( 100, 101 )
  • );
  •  
  •  
  • // -------------------------------------------------- //
  • // -------------------------------------------------- //
  •  
  •  
  • // String.substring( from, to )
  •  
  • // Let's start by using both begin and end.
  • console.log(
  • "substring( 3, 7 ) :",
  • numbers.substring( 3, 7 )
  • );
  •  
  • // What happens when we start with a negative number.
  • console.log(
  • "substring( -7, 7 ) :",
  • numbers.substring( -7, 7 )
  • );
  •  
  • // What happens when we use two negative numbers.
  • console.log(
  • "substring( -7, -3 ) :",
  • numbers.substring( -7, -3 )
  • );
  •  
  • // What happens when we omit the last argument.
  • console.log(
  • "substring( 3 ) :",
  • numbers.substring( 3 )
  • );
  •  
  • // And with the negative, end-relevant index.
  • console.log(
  • "substring( -7 ) :",
  • numbers.substring( -7 )
  • );
  •  
  • // If the index is out of range, it returns the empty string.
  • console.log(
  • "substring( 100, 101 ) :",
  • numbers.substring( 100, 101 )
  • );
  •  
  •  
  • // -------------------------------------------------- //
  • // -------------------------------------------------- //
  •  
  •  
  • // String.substr( start, length )
  •  
  • // Let's start by using both start and length
  • console.log(
  • "substr( 3, 4 ) :",
  • numbers.substr( 3, 4 )
  • );
  •  
  • // What happens when we start with a negative number.
  • console.log(
  • "substr( -7, 4 ) :",
  • numbers.substr( -7, 4 )
  • );
  •  
  • // What happens when we omit the last argument.
  • console.log(
  • "substr( 3 ) :",
  • numbers.substr( 3 )
  • );
  •  
  • // And with the negative, end-relevant index.
  • console.log(
  • "substr( -7 ) :",
  • numbers.substr( -7 )
  • );
  •  
  • // If the index is out of range, it returns the empty string.
  • console.log(
  • "substr( 100, 1 ) :",
  • numbers.substr( 100, 1 )
  • );
  •  
  •  
  • </script>
  • </head>
  • <body>
  • <!-- Intentionally left blank. -->
  • </body>
  • </html>
slice( 3, 7 ) : 3456
slice( -7, 7 ) : 3456
slice( -7, -3 ) : 3456
slice( 3 ) : 3456789
slice( -7 ) : 3456789
slice( 100, 101 ) :
substring( 3, 7 ) : 3456
substring( -7, 7 ) : 0123456
substring( -7, -3 ) :
substring( 3 ) : 3456789
substring( -7 ) : 0123456789
substring( 100, 101 ) :
substr( 3, 4 ) : 3456
substr( -7, 4 ) : 3456
substr( 3 ) : 3456789
substr( -7 ) : 3456789
substr( 100, 1 ) :
As you can see, the slice() and substring() methods are roughly the same; the only difference is that the slice() method can accept a negative index, relative to the end of the string. Should you try to use a negative index in substring(), it seems to be relative the beginning of the string (and therefore is out-of-bounds).
The substr() method can use a negative index; but, rather than using a to-argument, it uses a length-argument. This can be especially useful if you know the length of the substring ahead of time and don't want to perform any superflous math to calculate the necessary indices.

Tuesday, 12 March 2013

What is jLINQ?


jLinq is a 100% JavaScript library that allows you to perform complex queries on arrays of JSON data. Instead of using for loops and if statements, you can write fluent queries to filter, sort and select the information you need.

What is jLINQ?

LINQ style functionality for Javascript. Allows you to work with sets of data using query style syntax to select, order and sort records.

The code is especially useful if you have medium sized sets of information that could be sorted or searched, but you don't want to have to use a server side call to resort your records.

So what does it look like?

If you've ever used LINQ before you'll be familiar with the syntax. The code below is a simple way to select records from an array in Javascript.

//Some sample data
var records = [
  {name:"John", age:25, admin:true},
  {name:"Mike", age:35, admin:false},
  {name:"Randal", age:41, admin:false},
  {name:"Stephanie", age:32, admin:true}
  ];

//And a sample query using jLINQ
var results = jLinq.from(records)
  .ignoreCase()
  .startsWith("name", "m")
  .or("j")
  .is("admin")
  .orderBy("age")
  .select();
}}

jLINQ simplifies working with arrays of information by providing simple commands to make selections.

Other Resources





Finding Password strength using Javascript


<!DOCTYPE HTML>
<html>
    <head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Password Strength Checking with jQuery - gazpo.com</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<style rel="stylesheet" type="text/css">
html, body, ul, ol, li, form, fieldset, legend{
margin: 0;
padding: 0;
}

body{
background: url('bg.png') repeat;
font-family: 'Vollkorn', serif, Arial;
font-size: 15px;
line-height: 1.5;
}

h1, h2, h3, h4, h5, h6, p {
margin: 0;
}

p{
margin-bottom:15px;
}

a{
color:#0073BF;
text-decoration:none;
}

#container{
width:600px;
margin:0 auto;
background:#ffffff;
padding:20px;
}

#header{
text-align:center;
margin:20px 0 40px;
}

#footer{
text-align:center;
margin-top:40px;
}

#register {
margin-left:100px;
}
#register label{
margin-right:5px;
}
#register input {
padding:5px 7px;
border:1px solid #d5d9da;
box-shadow: 0 0 5px #e8e9eb inset;
width:250px;
font-size:1em;
outline:0;
}

#result{
margin-left:5px;
}

#register .short{
color:#FF0000;
}

#register .weak{
color:#E66C2C;
}

#register .good{
color:#2D98F3;
}

#register .strong{
color:#006400;
}
</style>
<script type="text/javascript">
 $(document).ready(function() {
$('#password').keyup(function(){
$('#result').html(checkStrength($('#password').val()))
})

function checkStrength(password){
 
//initial strength
    var strength = 0

    //if the password length is less than 6, return message.
    if (password.length < 6) {
$('#result').removeClass()
$('#result').addClass('short')
return 'Too short'
}
 
    //length is ok, lets continue.

//if length is 8 characters or more, increase strength value
if (password.length > 7)
strength += 1

//if password contains both lower and uppercase characters, increase strength value
if (password.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/))
strength += 1

//if it has numbers and characters, increase strength value
if (password.match(/([a-zA-Z])/) && password.match(/([0-9])/))
strength += 1

//if it has one special character, increase strength value
    if (password.match(/([!,%,&,@,#,$,^,*,?,_,~])/))
strength += 1

//if it has two special characters, increase strength value
    if (password.match(/(.*[!,%,&,@,#,$,^,*,?,_,~].*[!,%,&,@,#,$,^,*,?,_,~])/))
strength += 1

//now we have calculated strength value, we can return messages

//if value is less than 2
if (strength < 2 ) {
$('#result').removeClass()
$('#result').addClass('weak')
return 'Weak'
} else if (strength == 2 ) {
$('#result').removeClass()
$('#result').addClass('good')
return 'Good'
} else {
$('#result').removeClass()
$('#result').addClass('strong')
return 'Strong'
}
}
});
</script>
</head>

<body>
<div id="container">

<div id="header">
<h2>Password Strength Checking with jQuery<h2>
</div>

<div id="content">
<!-- form start-->
<form id="register">
<label for="password">Password : </label>
<input name="password" id="password" type="password"/>
<span id="result"></span>
</form>
<!-- form end-->
</div>

</div>
</body>
</html>

Friday, 1 February 2013

dhtmlxSuite is a rich JavaScript library that delivers a complete set of UI components
http://dhtmlx.com/