Skip to content

Instantly share code, notes, and snippets.

@CiaraBurkett
Created May 1, 2014 13:33
Show Gist options
  • Save CiaraBurkett/3e37bfd8c8f3ad215393 to your computer and use it in GitHub Desktop.
Save CiaraBurkett/3e37bfd8c8f3ad215393 to your computer and use it in GitHub Desktop.
Utilities.js from Larry Ullman's Modern JavaScript
// Utilities.js from Larry Ullman's Modern JavaScript
var U = {
$: function(id) {
'use strict';
if (typeof id == 'string') {
return document.getElementById(id);
}
}, // end $()
setText: function(id, message) {
'use strict';
if ( (typeof id == 'string') && (typeof message =='string') ) {
var output = this.$(id);
if (!output) return false;
if (output.textContent !== undefined) {
output.textContent = message;
}
else {
output.innerText = message;
}
return true;
} // end main if
}, // end setText()
addEvent: function(obj, type, fn) {
'use strict';
if (obj && obj.addEventListener) {
obj.addEventListener(type, fn, false);
}
else if (obj && obj.attachEvent) {
obj.attachEvent('on' + type, fn);
}
}, // end addEvent()
removeEvent: function(obj, type, fn) {
'use strict';
if (obj && obj.removeEventListener) {
obj.removeEventListener(type, fn, false);
}
else if (obj && obj.detachEvent) {
obj.detachEvent('on' + type, fn);
}
} // end of removeEvent()
}; // end of U declaration
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment