venerdì, dicembre 02, 2005

10 funzioni base Javascript ^_^;;

sono rimasto attratto da Questo post a tal punto che ho deciso di segnalarlo,
ma non ero pienamente soddisfatto del codice segnalato, cosi' ho deciso di ottimizzarlo un attimino ;)

/* Reference Article:
Dustin Diaz:
http://www.dustindiaz.com/top-ten-javascript/



Optimizations made by kentaromiura, you could implement duff faster device for more optimization if you would.
http://mykenta.blogspot.com
*/


/* addEvent: simplified event attachment */
function addEvent( obj, type, fn ) {
if (obj.addEventListener) {
obj.addEventListener( type, fn, false );
EventCache.add(obj, type, fn);
}
else if (obj.attachEvent) {
obj["e"+type+fn] = fn;
obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
obj.attachEvent( "on"+type, obj[type+fn] );
EventCache.add(obj, type, fn);
}
else {
obj["on"+type] = obj["e"+type+fn];
}
}

var EventCache = function(){
var listEvents = [];
return {
listEvents : listEvents,
add : function(node, sEventName, fHandler){
listEvents.push(arguments);
},
flush : function(){
var item;
var i= listEvents.length - 1;
//for(i = listEvents.length - 1; i >= 0; i = i - 1)
if(i!=-1)
do{
item = listEvents[i];
if(item[0].removeEventListener){
item[0].removeEventListener(item[1], item[2], item[3]);
};
if(item[1].substring(0, 2) != "on"){
item[1] = "on" + item[1];
};
if(item[0].detachEvent){
item[0].detachEvent(item[1], item[2]);
};
item[0][item[1]] = null;
}while(--i>-1);
}
};
}();
addEvent(window,'unload',EventCache.flush);

////////////// window 'load' attachment

/*
function addLoadEvent(func) {
var oldonload = window.onload;

if (typeof window.onload != 'function') {
window.onload = func;
}
else {
window.onload = function() {
oldonload();
func();
}
}
}
*/
function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof oldonload != 'function') {
window.onload = func;
}
else {
window.onload = function() {
oldonload();
func();
}
}
}

/* grab Elements from the DOM by className */
function getElementsByClass(searchClass,node,tag) {
var classElements = new Array();
if ( node == null )
node = document;
if ( tag == null )
tag = '*';
var els = node.getElementsByTagName(tag);
var elsLen = els.length;
var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");

//AAARGH ORROR i && j are defined global!!
//THIS SLOW DOWN the code and would cause weird behaviour !!(think in a Multithreading program!!)

/*for (i = 0, j = 0; i < i =" 0;" j =" 0;" return="" classelements="" toggle="" an="" element="" s="" display="" function="" obj="" var="" el="" would="" use="" switch="" for="" more="" optimization="" but="" i="" think="" is="" ininfluent="" due="" to="" the="" shorten="" dom="" trick="" already="" used="">
if ( el.display != 'none' ) {
//alert('ok');
el.display = 'none';
}
else {
el.display = '';
}
}
}

/* insert an element after a particular node */
/*function insertAfter(parent, node, referenceNode) {
parent.insertBefore(node, referenceNode.nextSibling);
}*/
function insertAfter(parent, node, referenceNode){

if(referenceNode.nextSibling)
{
parent.insertBefore(node, referenceNode.nextSibling);
}
else
{
parent.appendChild(node);
}
}
/*
I prefer the following form ;) however this function throw an error when node.parentNode don't exist (eg.when you don't pass a DOM-Node ;)

function insertAfter2(node, referenceNode){

if(referenceNode.nextSibling)
{
referenceNode.parentNode.insertBefore(node, referenceNode.nextSibling);
}
else
{
referenceNode.parentNode.appendChild(node);
}
}
*/
/* Array prototype, matches value in array: returns bool */
Array.prototype.inArray = function (value) {

var i=this.length-1;
if(i>-1)
do
{
//alert('i'+i+')'+this[i])
if (this[i] === value) {
return true;
}
}
while(--i>-1);

return false;
};

/* get, set, and delete cookies */
function getCookie( name ) {
var cook=document.cookie
var start = cook.indexOf( name + "=" );
var len = start + name.length + 1;
if ( ( !start ) && ( name != cook.substring( 0, name.length ) ) ) {
return null;
}
if ( start == -1 ) return null;
var end = cook.indexOf( ";", len );
if ( end == -1 ) end = cook.length;
return unescape( cook.substring( len, end ) );
}

function setCookie( name, value, expires, path, domain, secure ) {
var today = new Date();
today.setTime( today.getTime() );
if ( expires ) {
//expires = expires * 1000 * 60 * 60 * 24;
expires*=86400000;
}
var expires_date = new Date( today.getTime() + (expires) );
document.cookie = name+"="+escape( value ) +
( ( expires ) ? ";expires="+expires_date.toGMTString() : "" ) + //expires.toGMTString()
( ( path ) ? ";path=" + path : "" ) +
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" );
}

function deleteCookie( name, path, domain ) {
if ( getCookie( name ) ) document.cookie = name + "=" +
( ( path ) ? ";path=" + path : "") +
( ( domain ) ? ";domain=" + domain : "" ) +
";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

/* quick getElement reference
look for the comment for the code ^_^; */