Functions find Left and Right position of an Object

When a layer is inside another layer, it’s style.left and style.top positions can both be ‘0’
but the layer itself can be 500 pixels away from the actual margin. These functions will return
the true offset.
// To find the left position, add this snippet to your code:

function getPositionLeft(This){
var el = This;var pL = 0;
while(el){pL+=el.offsetLeft;el=el.offsetParent;}
return pL
}

// To find the top position, add this snippet to your code:

function getPositionTop(This){
var el = This;var pT = 0;
while(el){pT+=el.offsetTop;el=el.offsetParent;}
return pT
}

Get all Indexes of Duplicate Entries in an array

Array.prototype.collectDuplicateIndices = function (value)
// Returns true if the passed value is found in the
// array. Returns false if it is not.

{
var i;
var dupesStr = “”;
for (i=0; i < this.length; i++) { // use === to check for Matches. ie., identical (===), ; if (this[i] == value) { dupesStr += i+","; } } return (dupesStr>“”)?dupesStr:”null”;
};

This will return all the index/positions of the duplicate entries in an Array as a string seperated with commas.

For example the Returned string will be 1,77,55
Where 1,77 and 55 are the positions of the duplicate entries in the array. ( Or you can say those positions are having the duplicate entries)

Check if an element or value exists in an array or not

This function will Check if an element or value exists in an array or not.
And return true if the passed value is found in the array. Returns false if it is not found in the array.

Array.prototype.inArray = function (value,caseSensitive)
// Returns true if the passed value is found in the
// array. Returns false if it is not.
{
var i;
for (i=0; i < this.length; i++) { // use === to check for Matches. ie., identical (===), if(caseSensitive){ //performs match even the string is case sensitive if (this[i].toLowerCase() == value.toLowerCase()) { return true; } }else{ if (this[i] == value) { return true; } } } return false; }; Note : If you don't want to perform case sensitive check, then set caseSensitve=true and call the function.

How to Disable the browsers’ back button

You can Disable the browsers’ back button if you want to prevent your users to go back for some reasons like the registration page or for some pages once they are submitted should not be reversed back, which might lead into unwanted problems .

There is a little hack to achieve this, you can use the following js code.


location.replace(yourURL)
window.history.forward(1);

Close Window in IE from javascript. (Small Hack)

When trying to close an window from javascript in IE, which is opened manually,
a security warning “The web page you are viewing is trying to close the window. Do you want to close this window? Yes|No ” is popping up. Is there any possibility to close a window silently? Yes.

A simple hack i found on the web will help to get rid of this situation.


this.focus();
self.opener = this;
self.close();

but in Firefox you can do this. Firefox will not allow you to close using the above code. Maybe anybody found/having any code post it here.

How to find your Popup is blocked by Popup blocker in javascript

How to find your Popup is blocked by Popup blocker in javascript when using window.open.

We open a window using window.open() in our pages using javascript. But if there is popup blocker sitting in the client machine and the user is not aware the popup blocker is blocking the popup window from your site, we can inform him the popup is blocked and ask him to remove that. How?


winHandle=window.open('yourfile.htm' + "?newwin=true", "_top", "width="+wwidth+",height="+wheight+",location=0,menubar=0,resizable=no,scrollbars=no,status=yes,titlebar=no,dependent=yes");
if(winHandle==null){
alert("Error: While Launching New Window...nYour browser maybe blocking up Popup windows. nn  Please check your Popup Blocker Settings");
}

How to Change an image’s width dynamically from Javascript

How to Change an image’s width dynamically from Javascript. Using DOM we can do this.

function changeImgSize(objectId,newWidth,newHeight) {
imgString = 'theImg = document.getElementById("'+objectId+'")';
eval(imgString);
oldWidth = theImg.width;
oldHeight = theImg.height;
if(newWidth>0){
theImg.width = newWidth;
}
if(newHeight>0){
theImg.height = newHeight;
}
}

Note: this one i found on the web but i can’t remember where i found. Credits to the author.

Check whether an HTML element/Object exists or not.

This small javascript function will help you to check if a HTML object.Element exists in a page from the page itself or from a popup window.


function chkObject(inParent,theVal) {
if(inParent){
if (window.opener.document.getElementById(theVal) != null) {
return true;
} else {
return false;
}
}else{
if (document.getElementById(theVal) != null) {
return true;
} else {
return false;
}
}
}

Note:
chks whether an object exists or not in the document.
if the inParent is true then the object/elt is checked for its existence in the parent/opener’s document.
i.e., it will be useful to chk from a popup window.
To check in the current document itself , then set inParent value as false.