Monday, 11 June 2012

What is the difference between the .bind() and .live() methods in jQuery?


.bind() will only apply to the items you currently have selected in your jQuery object. .live() will apply to all current matching elements, as well as any you might add in the future.

The underlying difference between them is that live() makes use of event bubbling. That is, when you click on a button, that button might exist in a <p>, in a <div>, in a <body> element; so in effect, you're actually clicking on all of those elements at the same time.

live() works by attaching your event handler to the document, not to the element. When you click on that button, as illustrated before, the document receives the same click event. It then looks back up the line of elements targeted by the event and checks to see if any of them match your query.

The outcome of this is twofold: firstly, it means that you don't have to continue reapplying events to new elements, since they'll be implicitly added when the event happens. However, more importantly (depending on your situation), it means that your code is much much lighter! If you have 50 <img> tags on the page and you run this code:
$('img').click(function() { /* doSomething */ });

 ...then that function is copied into each of those elements. However, if you had this code:
$('img').live('click', function() { /* doSomething */ });

 ...then that function is stored only in one place (on the document), and is applied to whatever matches your query at event time.

Because of this bubbling behaviour though, not all events can be handled this way.

// BIND example 
$('div').bind('mouseover', doSomething); 
// this new div WILL NOT HAVE mouseover event handler registered 
$('<div/>').appendTo('div:last'); 
 
// LIVE example 
$('div').live('mouseover', doSomething); 
// this new appended div WILL HAVE mouseover event handler registered 
$('<div/>').appendTo('div:last');


Friday, 8 June 2012

Sending form submissions to email using 'mailto:'

The simplest method to get the form submissions posted to you via email is by using "mailto:" in the action field of the form. However, this method has many drawbacks.
First let us see how we can use 'mailto' in a form.


How to set up 'mailto' in your HTML form
In the action field of the form, set the action as "mailto:<youremailaddress>" for example:

<form action="mailto:myforms@mydomain.com">
The sad part is that the behavior of such a form will be different in different browsers.


How to get the form data in plain text
We can make the form data sent to be in readable form by making a small change in the form.
Add 'enctype=text/plain' in the form attributes. The form code becomes:


<form action="mailto:myforms@mydomain.com" enctype="text/plain" >

When this form is submitted, you will get the email in readable form. Like this:


Name=john
Email=john@yahoo.com
Address=
Submit=Submit


How to show a 'Thank You' page
You have successfully submitted the form. Now you want to show a confirmation page to the visitor that the form is submitted. Here is how to do it:
Suppose the name of the confirmation page is thanks.html Update the code for the form tag to:


<form action="mailto:myforms@mydomain.com" enctype="text/plain"onsubmit="location.href='thanks.html';" >


How to customize the subject of the email
By default, the subject of the email that you receive from such a form will read something like: "Form posted from Microsoft Internet Explorer"
The following code shows the change:

<form action= "mailto:myforms@mydomain.com?subject=myform_submission" enctype="text/plain" onsubmit="location.href='thanks.html';" >


Drawbacks of 'mailto' email form
We have seen that using mailto is simplest way to get your HTML form submissions via email.
However, there are many disadvantages for this method.

  1. Your visitor must have Internet Explorer as the browser and Outlook Express as the default client for this to work correctly.
  2. Even if your visitor is using Internet Explorer, but the default mail client is different, your mailto form will not work.
  3. Even if your visitors have IE and has configured Outlook Express as default mail client, they may press cancel on the warning dialog that pops up.
  4. According to the HTML specifications, the action field of the form should be a correctly formed HTTP URL. So 'mailto:' is not valid for the action field.
Use a form mail script
The only consistent alternative to ensure that you get the email form the form submissions is to use a form mail script.

Thursday, 7 June 2012

Difference between GROUP BY and PARTITION BY


GROUP BY modifies the entire query, like:
Select customerId, count(*) as orderCount 
from Orders 
group by customerId 

But PARTITION BY just works on a window function, like row_number:
select row_number() over (PARTITION BY customerId order by orderId) 
    as OrderNumberForThisCustomer 
from Orders 
A Group By normally reduces the number of rows returned by rolling them up and 
calculating averages or sums for each row.  Partition By does not affect the number of rows 
returned, but it changes how a window function's result is calculated.



Wednesday, 6 June 2012

SQL SERVER - APPLY


  1. An APPLY is used to join a table-valued-function with a table. APPLY are of two types: CROSS APPLY and OUTER APPLY
    A CROSS APPLY is used when you want to return only those records from your table where a value is returned by your table valued function.
    Whereas, an OUTER APPLY is used to return all records from your table regardless of the values returned by your table valued function.



    SELECT name.User_Id,name.UserName,name.Email,name.MobileNo,name.VisitedDate
    FROM allocate_site lead
    outer apply dbo.fn_get_latest_response_userwise(lead.User_ID) sales 
    cross apply dbo.fn_get_user_details(lead.User_ID) name
    inner join UserDetails users on users.ID = lead.User_ID
    WHERE lead.ProductId = name.ProductID