Created
May 1, 2014 13:33
-
-
Save CiaraBurkett/3e37bfd8c8f3ad215393 to your computer and use it in GitHub Desktop.
Utilities.js from Larry Ullman's Modern JavaScript
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
// 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