Friday, September 18, 2009

JS Array External Functions

Add This Functions in Your Script File
if( typeof Array.prototype.copy==='undefined' )
{
var a = [], i = this.length;
while( i-- ) {
a[i] = typeof this[i].copy!=='undefined' ? this[i].copy() : this[i];
}
return a;
};
if( typeof Array.prototype.remove==='undefined' ) {
Array.prototype.remove = function(obj) {
var a = [];
for (var i=0; i
{ if (this[i] != obj)
{ a.push(this[i]); }
} return a;}
}
How To Use
var ary=[1,2,3,4];
var copyary=ary.copy();
alert(copyary);
ary.remove(3);
alert(copyary); alert(ary);

JS util functions

function isArray(ob) {
// return ob.constructor == Array;
var ret = false;
try {
ret = ob.length !== undefined;
}
catch(e) {
ret = false;
}
return ret;
}
function isObject(ob) {
return typeof ob == "object";
}
function isFunction(ob) {
return typeof ob == "function";
}
function isString(ob) {
return typeof ob == "string";
}
function isNumber(ob) {
return typeof ob == "number";
}

JS Function To Get Clone Of Object

Way to Clone A Object In JavaScript

var ObjectHandler = {
//public method
getCloneOfObject: function(oldObject) {
var tempClone = {};
if (typeof(oldObject) == "object")
{
for (prop in oldObject)
{
// for array use private method getCloneOfArray
if ((typeof(oldObject[prop]) == "object") &&(oldObject[prop]).__isArray)
tempClone[prop] = this.getCloneOfArray(oldObject[prop]);
// for object make recursive call to getCloneOfObject
else if (typeof(oldObject[prop]) == "object")
tempClone[prop] = this.getCloneOfObject(oldObject[prop]);
// normal (non-object type) members
else
tempClone[prop] = oldObject[prop];
}
return tempClone;
}
},
//private method (to copy array of objects) - getCloneOfObject will use this internally
getCloneOfArray: function(oldArray) {
var tempClone = [];
for (var arrIndex = 0; arrIndex <= oldArray.length; arrIndex++){
if (typeof(oldArray[arrIndex]) == "object")
tempClone.push(this.getCloneOfObject(oldArray[arrIndex]));
else tempClone.push(oldArray[arrIndex]); return tempClone;
}
}
};

How To Use

var eventDetail = { description:"",summary:"",startTime:new Date(),endTime:new Date(),location:""};

Check What will Happen If you use normal way:

var copyOfeventDetail=eventDetail;
copyOfeventDetail.description="New Description";
alert("Copy description"+copyOfeventDetail.description+"Original Description" +eventDetail.description);

Check What will Happen If you use cloning way:
var cloneOfeventDetail =ObjectHandler.getCloneOfObject(eventDetail);
cloneOfeventDetail .description="New Description";
alert("Clone description"+cloneOfeventDetail .description+"Original Description" +eventDetail.description);

JS Function to find exact position on a Element

function findPositionWithScrolling( oElement ) {
function getNextAncestor( oElement ) {
var actualStyle;
if( window.getComputedStyle ) {
actualStyle = getComputedStyle(oElement,null).position;
} else if( oElement.currentStyle ) {
actualStyle = oElement.currentStyle.position;
} else {
//fallback for browsers with low support - only reliable for inline styles
actualStyle = oElement.style.position;
}
if( actualStyle == 'absolute' actualStyle == 'fixed' ) {
//the offsetParent of a fixed position element is null so it will stop
return oElement.offsetParent;
}
return oElement.parentNode;
}
if( typeof( oElement.offsetParent ) != 'undefined' ) {
var originalElement = oElement;
for( var posX = 0, posY = 0; oElement; oElement = oElement.offsetParent ) {
posX += oElement.offsetLeft;
posY += oElement.offsetTop;
}
if( !originalElement.parentNode !originalElement.style typeof( originalElement.scrollTop ) == 'undefined' ) {
//older browsers cannot check element scrolling
return [ posX, posY ];
}
oElement = getNextAncestor(originalElement);
while( oElement && oElement != document.body && oElement != document.documentElement ) {
posX -= oElement.scrollLeft;
posY -= oElement.scrollTop;
oElement = getNextAncestor(oElement);
}
return [ posX, posY ];
} else {
return [ oElement.x, oElement.y ];
}
}
How To Use
<html>
<head>
<script>
var divObj=document.getElemeById('mydiv');
alert(findPositionWithScrolling(divOb'));
</script>
</head>
<body>
<div style="padding:20px;width:100px;height:100px">
<div style="padding:20px;width:5;height:5">

<div id="mydiv" style="padding:20px;width:20;height:2" >
</div>

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