Friday, December 12, 2008

Javascript to close the current window

"<script language='javascript'>window.open('','_parent','');window.close();</script>"

Javascript Form Submit Using POST action Into a new Window

<form> element of html have several attributes for controlling submission

method : get or post
action : the url for the post back action
target : the target for the post back action, _blank, _parent, _self, _top, name of a window
name : name of the form

In this topic of using javascript to change POST action of a form so that it post into a new Window, action and target is the most important attribute here.

you can use javascript function window.open(url, name, param)
e.g. window.open(test.html,'','scrollbars=no,menubar=no,height=600,width=800,resizable=yes,toolbar=no,status=no');

to post into another window, we will set the action to the url of the new window and target is the name of the new window.
e.g.

function PostToNewWindow()
{
    form.action = 'newForm.asp?action=submit&param=new'; //any valid url and query string
    form.target = 'NewPopupForm'; // name of the new form
    window.open('', 'NewPopupForm', 'height=50, width=50, resizable = no, toolbar=no, status=no');
    form.submit;

}

Wednesday, December 10, 2008

Trim a string in JavaScript using replace function and regular expression

from this article http://www.nicknettleton.com/zine/javascript/trim-a-string-in-javascript

var trimmed = str.replace(/^\s+|\s+$/g, '') ;

Tuesday, December 2, 2008

SQL SERVER SQL Query Conditional Count with CASE Clause inside COUNT

Suppose you have a master and child table in the following structure and you want to find out how many thing of each category is sold or not

Category       
Music       
Book       
Movie       


Category    Name    Sold
Music    Love Song    T
Music    Rock    T
Music    Classical    F
Book    Dictionary    T
Book    Fiction    T
Book    Textbook    F
Movie    James Bond    F
Movie    MI3    F


You will write the following query for it

Select
    Category
    ,Count(Name) as ProductCount
    ,Count(Case When Sold = 'T' Then Name Else NULL End) as SoldCount
    ,Count(Case When Sold = 'F' Then Name Else NULL End) as UnsoldCount
From Product
Group By Category

NOTE: the NULL value in the else clause is critical, if you put zero there, the query will not work.