Dustin Diaz has made a compilation of the best features in JavaScript, when I started reading this article I thought, "Will you have 10 more like him ..." I finished reading it and I applaud their choice, without a doubt the best or at least the most useful.
This can not be applied to all projects and that each will require some other functions, always dependiende of our needs. What if it is true that these functions are generalized execepcionalmente good, I have hence the reason why most large bookstores or integrate the same with a slight modification.
Javascript Example::addEvent()
function addEvent(elm, evType, fn, useCapture)
{
if (elm.addEventListener)
{
elm.addEventListener(evType, fn, useCapture);
return true;
}
else if (elm.attachEvent)
{
var r = elm.attachEvent('on' + evType, fn);
return r;
}
else
{
elm['on' + evType] = fn;
}
}
We started strong, this function is responsible for helping to manage the events of our website, it can assign to any one event, with the contents of the javascript function without obstruction is a reality. Created by Andrew Scott.
Javascript Example::addLoadEvent()
function addLoadEvent(func)
{
var oldonload = window.onload;
if (typeof window.onload != 'function')
{
window.onload = func;
}
else
{
window.onload = function()
{
oldonload();
func();
}
}
}
This is only a version for the object in the window onload event, so we can assign a number of features to our onload event and see the result when the burden of our website. It is also possible to use addEvent () to do the same. Created by Simon Willison.
Javascript Example::getElementsByClass()
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|$)');
for (i = 0, j = 0; i < elsLen; i++)
{
if ( pattern.test(els[i].className) )
{
classElements[j] = els[i];
j++;
}
}
return classElements;
}
A great role with a name of class, we can find the elements that constitute them, returning an array of objects to engage with them more comfortable. Originally was not created by anyone in particular but with time the developers have been adding improvements to debug the function.
7 – cssQuery()
Perhaps the best partner to work with CSS from Javascript, written by Dean Edwars draws attention to the number of selectors that allows you to use at the time of consultation. See documentation.
Javascript Example::toggle()
function toggle(obj)
{
var el = document.getElementById(obj);
if ( el.style.display != 'none' )
{
el.style.display = 'none';
}
else
{
el.style.display = '';
}
}
This function is implemented will have a million times, is the typical one that is used many times and generally do not know neither her name. This is a function that hides / shows an element depending on its previous state, osea shown what is hidden and vice versa.
Javascript Example::insertAfter()
function insertAfter(parent, node, referenceNode)
{
parent.insertBefore(node, referenceNode.nextSibling);
}
This had not ever used, created by Jeremy Keith, we can insert a node after another parent element.
Javascript Example::inArray()
Array.prototype.inArray = function (value)
{
var i;
for (i=0; i < this.length; i++)
{
if (this[i] === value)
{
return true;
}
}
return false;
};
How many times have you had to go through an entire array to see if there is a value or not? Because this function makes the search for you.
Javascript Example::getCookie()
function getCookie( name )
{
var start = document.cookie.indexOf( name + "=" );
var len = start + name.length + 1;
if (( !start ) && ( name != document.cookie.substring( 0, name.length))) {
return null;
}
if ( start == -1 ) return null;
var end = document.cookie.indexOf( ';', len );
if ( end == -1 ) end = document.cookie.length;
return unescape( document.cookie.substring( len, end ) );
}
Manual function to collect data from the cookie from the client, very comfortable and convenient.
Javascript Example::setCookie()
function setCookie( name, value, expires, path, domain, secure )
{
var today = new Date();
today.setTime( today.getTime() );
if ( expires )
{
expires = expires * 1000 * 60 * 60 * 24;
}
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' : '' );
}
A companion function getCookie () with which you can store the customer data in the form of a cookie.
Javascript Example::deleteCookie()
Mención Honorífica – $()
function $() {
var elements = new Array();
for (var i = 0; i < arguments.length; i++) {
var element = arguments[i];
if (typeof element == 'string')
element = document.getElementById(element);
if (arguments.length == 1)
return element;
elements.push(element);
}
return elements;
}
Function responsible for deleting cookies on the client.
By far the most prevalent feature of the network, all the used bookstores and even have added functionality (such as XPath in jQuery), getting a very pontente we return an item or a series of elements, only going as pelementos desired.