Created
September 1, 2011 08:48
-
-
Save nikoheikkila/1185737 to your computer and use it in GitHub Desktop.
Javascript: Utility library
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* utils.js | |
* | |
* @author Niko Heikkilä (@ytserman) <http://scr.im/ytserman> | |
* | |
* Contains several utility functions for cross-browser compatibility. | |
* Drop this into your javascript folder and call before other scripts. | |
* | |
*/ | |
function createRequest() { | |
/** | |
* createRequest | |
* | |
* Creates necessary XHR to use with AJAX apps if possible. | |
* Supports all browsers. | |
* | |
* @return XMLHTTPRequest || null | |
*/ | |
try { | |
request = new XMLHttpRequest(); | |
} catch (tryMS) { | |
try { | |
request = new ActiveXObject("Msxml2.XMLHTTP"); | |
} catch (otherMS) { | |
try { | |
request = new ActiveXObject("Microsoft.XMLHTTP"); | |
} catch (failed) { | |
request = null; | |
} | |
} | |
} | |
return request; | |
} | |
function isArray(arg) { | |
/** | |
* isArray | |
* | |
* Checks if passed object is an array | |
* | |
* @param arg Object to check | |
* @return bool | |
*/ | |
if (typeof arg == 'object') { | |
var criteria = arg.constructor.toString().match(/array/i); | |
return (criteria != null); | |
} | |
return false; | |
} | |
function addEventHandler(obj, eventName, handler) { | |
/** | |
* addEventHandler | |
* | |
* Attach event handlers the way your browser likes. | |
* NOTE: IE 9 supports DOM Level 2. | |
* | |
* @param obj object to attach to | |
* @param eventName name of the event | |
* @param handler handler function to run | |
* @return void | |
*/ | |
if (document.attachEvent) { | |
/* For browsers that don't support DOM Level 2. */ | |
obj.attachEvent("on" + eventName, handler); | |
} else if (document.addEventListener) { | |
/* For modern browsers. */ | |
obj.addEventListener(eventName, handler, false); | |
} | |
} | |
function getActivatedObject(e) { | |
/** | |
* getActivatedObject | |
* | |
* Function takes the passed argument from browser | |
* and figures out which activated object to return | |
* | |
* @param e Event object for handlers | |
* | |
* @return obj Desired activated object | |
*/ | |
var obj; | |
if (!e) { | |
/* Early versions of IE */ | |
obj = window.event.srcElement; | |
} else if (e.srcElement) { | |
/* IE 7 or later */ | |
obj = e.srcElement; | |
} else { | |
/* DOM Level 2 browsers */ | |
obj = e.target; | |
} | |
return obj; | |
} | |
function fieldIsFilled(e) { | |
var me = getActivatedObject(e); | |
if (me.value == "") { | |
warn(me, "required"); | |
} else { | |
unwarn(me, "required"); | |
} | |
} | |
function emailIsProper(e) { | |
var me = getActivatedObject(e); | |
var properEmail = /^[\w\.-_\+]+@[\w-]+(\.\w{2,4})+$/; | |
if ( ! properEmail.test(me.value)) { | |
warn(me, "format"); | |
} else { | |
unwarn(me, "format"); | |
} | |
} | |
function fieldIsLetters(e) { | |
var me = getActivatedObject(e); | |
var nonAlphaChars = /[^a-zA-Z]/; | |
if (nonAlphaChars.test(me.value)) { | |
warn(me, "letters"); | |
} else { | |
unwarn(me, "letters"); | |
} | |
} | |
function fieldIsNumbers(e) { | |
var me = getActivatedObject(e); | |
var nonNumericChars = /[0-9]/; | |
if (nonNumericChars.test(me.value)) { | |
// Error | |
} else { | |
// No. | |
} | |
} | |
/* End of file utils.js */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment