My top 10 JavaScript Blogs

Originally posted on JavaScript, JavaScript...:

These are the JavaScript blogs that I am repeatedly drawn to. Their emphasis is on the language itself. Real code, real situations. Enjoy!

Dmitry Baranovskiy, author of famed Raphael vector graphics engine. Updated fairly infrequently but always high quality
http://dmitry.baranovskiy.com/

Juriy Zaytsev (aka kangax) part of the Prototype.js team and all-round JavaScript good-guy. JavaScript wisdom from the master. Also best URL ever.
http://perfectionkills.com/

Alex Young et al. My first stop every morning. True to the URL – new stuff daily. Crystal clear JavaScript commentary and new product info. Added bonus – every Thursday Alex talks you through building his very own JavaScript framework.
http://dailyjs.com/

View original 226 more words

iOS 6 will service POST requests from it’s cache instead of sending them to the server

Currently I was working on a Web application.As these days most client want their application to be compatible with mobile web browser also.

We have to make the web application to be run properly in android , iPhone and Windows phone.
By the way I forget to tell you we were using the jQuery and WCF services in our project.
We were calling those WCF services via the jQuery ajax.

It was working properly in Android and windows-phone but somehow it was not working in iPhone.

The bug is that iOS 6 will service POST requests from its cache instead of sending them to the server.

let me explain this further
If we calling a WCF or Web service for the first time with some parameter.
the iOS browser caches the result that is return by the ajax calls.

If we call the service again with the same parameter then
Its showing the cached result not the updated result from the server.

After surfing through the web for several hours
finally found something to fix this bug.

There was 2 ways to resolve this.

  •  While creating the service put the below line in class constructor.

 HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);

  • While calling the service through ajax call. Put this line in ajax parameter

 headers: { “cache-control”: “no-cache” }

For example ,

$.ajax({
cache: false,
async: true,
type: "POST",
headers: { "cache-control": "no-cache" },
url: "http://WCFService/getAddress",
data: JSON.stringify({ personID: 1 }),
contentType: "application/json; charset=utf-8",
dataType: "json",

});

If you have so many ajax call then you can set header options in ajaxSetup also. Like below:-


$.ajaxSetup({
type: 'POST',
headers: { "cache-control": "no-cache" }
});

 

Difference between using Var and not using Var in javascript.

If you are using in global scope then there is no difference.
But It you are in a function then var will create a local variable.
variable define without the var keyword will look up the scope chain until it find the variable otherwise it will become a global variable.

var foo=1;
bar=1;

Above both are global variables

function Calc(){
var foo=1; // local variable
bar = 1;   // global variable
}

Please note that if you are not doing any assignment then you need to use var.

var x; // declare x variable

Ranking function in SQL Server 2008

SQL server 2008 provide many new and useful functions that we can use.
Some of them are known as Ranking functions.

Mainly there are 3 ranking functions.
1)Row_Number
2)Rank
3)Dense_Rank

Row_Number:-
Returns a unique number for each row starting with 1. For rows that have duplicate values,numbers are arbitarily assigned.

Rank:-
Assigns a unique number for each row starting with 1,except for rows that have duplicate values,in which case the same ranking is assigned and a gap appears in the sequence for each duplicate ranking.

Dense Rank:-
This is same as RANK() function. Only differencec is returns rank with out gaps.

Lets create a table and put some data to see what these functoin actually do.
We have created a table with the Name BookTable.
Image

We have put the following dummy data for testing.

Image

Now we have created following two query.

Image

The result of the above query is like this.

Image

As you analyses the result you can understand how these ranking function works.

Row_number will returns a unique number for each row starting with 1.

Rank will assigns a unique number for each row starting with 1,except for rows that have duplicate values and a gap appears in the sequence for each duplicate ranking.

Dense Rank is same as RANK() function. Only differencesc is returns rank with out gaps.

Javascript and jquery loop performance

if you use javascript or jquery regularly in your web project.
Then you may have come across a situation where you have to loop through the a javascript object or array.
There are many ways you can loop through the objects using javascript or jquery.
Today I decided to do a test page to measure the performance of different loop in different browsers.

The first loop is like this
for(var key in objects){}

2nd loop is
for(i=0;i<=objects.length-1;i++){}

and 3rd on is using jquery
$.each(object,function(index,value){}

First I have created a function which will add 50,000 items into a object

function addItems() {
for (var i = 0; i <= 50000; i++) {
object.push('Hello-' + i);
}
}

Then we will loop through this object using each loop method.

  • 1st Method
function loopFor() {
newObject = [];
for (var key in object) {
if (object.hasOwnProperty(key)) {
newObject.push(object[key]);
}
}
}
  • 2nd Method
function loopIn() {
newObject = [];
for (var i = 0; i <= object.length - 1; i++) {
newObject.push(object[i]);
}
}
  • 3rd Method ( jquery)
function jQueryloop() {
newObject = [];
$.each(object, function (i, value) {
newObject.push(object[i]);
});
}

I have tested the execution time taken by each method i 3 different browsers.Internet Explorer, Firefox and chrome. And here is the result of

  • Chrome

chrome

  • Internet Explorer

IE

  • Firefox

firefox

Below  is the full code

<table width="15%">
<tr>
<td>
Javascript For each loop -
</td>
<td>
<label id="lblLoopFor">
</label>
</td>
</tr>
<tr>
<td>
Javascript For Normal loop -
</td>
<td>
<label id="lblLoopIn">
</label>
</td>
</tr>
<tr>
<td>
JQuery For each loop -
</td>
<td>
<label id="lbljQueryLoop">
</label>
</td>
</tr>
</table>
$(document).ready(function () {
var object = [];
var newObject = [];

addItems(); //Add 50,000 items to objects

//Count Timing for the first function
var start = new Date().getTime();
loopFor();
var end = new Date().getTime();
$("#lblLoopFor").text(end - start);

//Count timing for the 2nd function
var start1 = new Date().getTime();
loopIn();
var end1 = new Date().getTime();
$("#lblLoopIn").text(end1 - start1);

//Count timing for the 2nd function
var start2 = new Date().getTime();
jQueryloop();
var end2 = new Date().getTime();
$("#lbljQueryLoop").text(end2 - start2);

function addItems() {
for (var i = 0; i <= 50000; i++) {
object.push('Hello-' + i);
}
}

function loopFor() {
newObject = [];
for (var key in object) {
if (object.hasOwnProperty(key)) {
newObject.push(object[key]);
}
}
}

function loopIn() {
newObject = [];
for (var i = 0; i <= object.length - 1; i++) {
newObject.push(object[i]);
}
}

function jQueryloop() {
newObject = [];
$.each(object, function (i, value) {
newObject.push(object[i]);
});
}
});

Jquery Ajax call works in all browser except IE 10.

The OperationFormatter could not deserialize any information from the Message because the Message is empty.”

If you are working with WCF and calling those services via jquery AJAX..
Running that application in IE10 you may get this error.

Error

 

Looking into this error you may think this is something related to jquery or WCF..
But it is not.

If you have install any download manage on the system like
Download Accelerator Plus or Free Download Manage
then uninstall it and try again.

So this is not a problem in jquery or WCF .Somehow these download managers (software) altering the behaviour of IE10.

What is CTE (common table expression) in sql server?

CTE (Common table expressions) are temporary result sets which is defined within the execution scope of the a single insert,select,update or create view statement.

A Common Table Expression contains three core parts:

  • The CTE name (this is what follows the WITH keyword)
  • The column list (optional)
  • The query (appears within parentheses after the AS keyword)

 

Some use of CTE:-

  • To find the 3rd highest salary

with cte as
(
Select Row_Number() over(order by salary desc) as RNO,* from Employee
)
Select * from cte where RNO=3  // you can use any name in place of cte.

 

  • Series with CTE

with ShowMessage(statement,length)
{
Select statement = cast(‘I like ‘ as varchar(300),len(‘I like ‘)
UNION ALL
Select cast(statement) + ‘Cte’ as varchar(300),len(statenent)
from ShowMessage
where length < 300
)

Select statement, length from showMessage

 

 

Coalesce function in Sql Server 2008

Coalesce function accept one or more columns name of the same data type.This function returns the first non null expression among its argumentsasd
example:-
Select Name,Coalesce(Address1,Address2,Address3) from Contact_info

This query will return the first none null value from the 3 columns which has been passed into it. If Addess1 is null then it will look into address2 column and if address2 is null then it will look into address3 column and again if address3 is null then it will return null value.
>>>>>>>>>>>>>>>>>>>>>>>>> another use of this function <<<<<<<<<<<<<<<<<<<<<<<<<<<<<

if you want to create a string which of all the rows value of a particular column then use this function like this,
For example Department column has following values:-

1) Technical
2)Support
3)HR

Then below query will return a result like this :-  Technical,Support,HR. It will combine all rows values of that particular column by “,”.

Declare @Department varchar(max)
Select @Department=Coalesce(@Department,”) + Name + ‘,’
from Human_Resources
>>>>>>>>>>>>>>>>>>>>>>>>> another use of this function <<<<<<<<<<<<<<<<<<<<<<<<<<<<<

To Kill all the active Transaction in the database use below query.

Declare @sql varchar(max)

Select @sql= Coalesce(@sql,”) + ‘Kill ‘ + cast(SPID as varchar(10)) + ‘;’
from Sys.SysProcesses
where DBID=DB_ID(‘NorthWind’)

Exec (@sql)
Print @sql

Javasciprt Trim fuction does not support in IE 6,IE 7,IE 8

As most of the developer hate Internet explorer browser including me for many reasons.
But recently I come across a problem related to IE browser.The javasciprt trim function does not support in IE 6, IE 7,IE 8.
So if you write somthing like this in javascipt.

val demo=”    This is a test    “;
demo.trim();

It will throw an error the “Object doesn’t support property or method ‘trim’
So to fixed this issue you can declare a prototype function like this.

if(typeof String.prototype.trim !== ‘function’) {
String.prototype.trim = function() {
return this.replace(/^\s+|\s+$/g, ”);
}
}

But if you are using jquery then you can use something like this.

val demo=”   This is a test  “;
$.trim(demo);

It will give you “This is a test”.