Thursday, February 28, 2008

Javascript HashMap Implementation

/*
Function to be used as
var hasmp1=Collection.HashMap();
var hasmp2=Collection.HashMap();
hasmp1.set('1','AAAAA1');
hasmp2.set('2','BBBBB2');
alert("MAP1 :"+hasmp1.get('1')+" MAP2:"+hasmp2.get('1'));
=========================================================================
Only a few Method is implemented as of Now.
  1. boolean isEmpty() Returns true if this map contains no key-value mappings.
  2. Set keySet() Returns a set view of the keys contained in this map.
  3. Object put(Object key, Object value) Associates the specified value with the specified key in this map.
  4. void putAll(Map m) Copies all of the mappings from the specified map to this map These mappings will replace any mappings that this map had for any of the keys currently in the specified map.
  5. Object remove(Object key) Removes the mapping for this key from this map if present.
  6. int size() Returns the number of key-value mappings in this map.
  7. Collection values() Returns a collection view of the values contained in this map.
==========================================================================
*/
var Collection={
HashMap:function(){
var hashMap = {
put : function(key,val) {this[key] = val;},
get : function(key) {return this[key];}
}
return hashMap;
}
}

Tuesday, February 26, 2008

HTML Button related Javascript Functions

1.How to create an IMAGE button through Javascript
Function Arguments
  1. srcImg:-the Default Image url
  2. mouseOverImg :- the mouse over Image url
function createIMGButton(srcImg,mouseOverImg){
var but=document.createElement("IMG");
but.src=srcImg;
but.onmouseover=new function(){but.src=mouseOverImg;}
return but;
}

2.HTML Button Mouse Listener.
Function Arguments
  1. but:-The HTML button Object
  2. mouseOverImg :- the mouse over Image url
  3. mouseOutImg:- the mouse out Image url

function buttonMouseListener(but,mouseOverImg,mouseOutImg){
but.onmouseover = function(){but.src=mouseOverImg;}
but.onmouseout = function(){but.src=mouseOutImg;}
}

3.Create HTML CheckBox
Function Arguments
  1. The id of the checkBox.
function createChkBox(id){
var checkBox = document.createElement("input");
checkBox.type = "checkbox"; // set type
checkBox.id = id;
return checkBox;
}

HTML Dropdown Handling Javascript Functions

1.How to Remove an Option From HTML DropDown [removeOptionDsc]
The following method will help to remove one option in the Drop down by sending the Description in the Drop down.
Function Arguments
dd:- The Drop down object
dsc:- the description in the drop down which is to be deleted.
function removeOptionDsc(dd,dsc){
for (var i = 0; i < dd.options.length; i++){
var opt = dd.options[i];
if(opt.childNodes(0).data==dsc){
dd.options[i]=null;
}
}
}

Example:
If one dropdown id is "users" with data
  1. Asker Ali M
  2. Haris M
  3. BrickRed
  4. Horizon
then user can call like
var ddObject= document.getElementById("users");
removeOptionDsc(ddObject,"Asker Ali M");

Then the dropdown will be
  1. Haris M
  2. BrickRed
  3. Horizon

2.To remove an Option by passing DropdownId.
Function Arguments
dd:- The Drop down object
val:- the option id which is to be deleted.

function removeOption(dd,val){
for (var i = 0; i < dd.options.length; i++){
var opt = dd.options[i];
if(opt.value==val){
dd.options[i]=null;
}
}
}

3.How get the description of the Option by passing optionId.
Function Arguments
dd:- The Drop down object
val:- the option id to retrieve the DescriptionReturn type:
Description String.

function getDDDsc(dd,val){
for (var i = 0; i < dd.options.length; i++){
var opt = dd.options[i];
if(opt.value==val){
return opt.childNodes(0).data;
}
}
}

4.How to clear a DropDown

Function Arguments
dd:- The Drop down object
function clearDD(dd){
if(dd==null) return;
var size = dd.options.length;
for (q = size; q >= 0; q--)
{
dd.options[q]=null;
}
}

Monday, February 25, 2008

Ajax Implementation Libraries.

Here i would like to introduce few JavaScript functions which will help to Implement Ajax easily in Any of the Pages.
I think no need to introduce about the Steps and basics of Ajax in this document.
for using this library what a developer has to do is create a global javascript file named like glbFunctions.js where we will write all the following javascript functions.

=========================================================
var xmlHttp;//Here we are keeping xmlHttp as the global variable to use every where.

function createXMLHttpRequestObject(){
if (window.ActiveXObject) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
}
}

this method will check the browser and the supported XMLHttpObject.

/*=========
This function will Make the ajax request with the method number given as
parameter with the request parameters.
The ready state function is the Argument function.
==========*/
function ajaxCall(responseFun,serverURL,reqParams,sync)
{
createXMLHttpRequestObject(); //Step 1
url = "serverURL"+"&"+reqParams; //Step 2
if(!validateUrl(url)) //Validating URL
return false;
xmlHttp.open("GET", url, sync);
if(responseFun!= null)
xmlHttp.onreadystatechange = function(){ajaxCallBack(responseFun);}
xmlHttp.send(null);
}

The ajaxCall is the public method which can be called from any where.
The method arguments are
  1. responseFun :The method in which is called when the Ajax response is Back.
  2. serverURL :The url of the server where this ajax call is Submitting.
  3. reqParams :The Request parameters of this request.
  4. sync :shows to make this ajax call synchronized or Asynchronized.
        1. Please refer XMLHttpObject.open() .Method in JavaScript.
The following method is the response controller of this Ajax,
The response required
/*========= Function will be called with the Array of details tags =============*/
function ajaxCallBack(responseFun) {
if (xmlHttp.readyState == 4) {
if (xmlHttp.status == 200) {
det = xmlHttp.responseXML.getElementsByTagName("details");
responseFun(det);
}
if (xmlHttp.status == 600) {//600 is the error value send from server side.
var msg=getHttpErrorMsg(xmlHttp.responseTEXT);//Userdefined method to get the Error value from response xml.
alert(msg);
}
}
}

The ajaxCallBack method will call the the responseFunct with the data from the response XML and will send back th the method.
The response should be like

User Data XML.

Example.
==========
A Simple jsp for Concatenation.
test.jsp
======<span style="font-weight: bold;"><br /></span>w Document

/*========= Function will be called with the Array of details tags =============*/
function ajaxCallBack(responseFun) {
if (xmlHttp.readyState == 4) {
if (xmlHttp.status == 200) {
det = xmlHttp.responseXML.getElementsByTagName('details');
responseFun(det);
}
if (xmlHttp.status == 600) {
var msg=getHttpErrorMsg(xmlHttp.responseTEXT);
alert(msg);
}
}
}


 Simple.jsp

==========
<%@ page language="java" %>
<%
String fName=request.getParameter("fname");
String sName=request.getParameter("sname");
String fullName=fName+" "+sName;
out.println("Hello
"+fullName+"
");

%>



An Eavening at Jamia Millia Islamia


Thank God.
It was a wonderful evening at Jamia Millia Islamia on 23rd February 08 with Musadik.I was really surprised by watching the Academic section library and Reading room.Thousands of students were reading in the Zakir Hussain Library which i was never ever seen such an Academic atmosphere.
me with Musadik and the student came to delhi for higher education went to different hostels and had the Tea from hostels.
It is a wonderful university for higher education.