/ Published in: JavaScript
Pass this an ID or an object reference, as well as any number of functions that take the referenced object as a parameter. This wrapper is intended to provide a sanity check against missing DOM elements.
Expand |
Embed | Plain Text
function safeGet () { var o; var ioj = arguments[0]; if (typeof ioj == 'string') { o = document.getElementById(ioj); } else { o = ioj; } if (typeof ioj != 'object') return; for (var i=1; i< arguments.length; i++) { arguments[i](o); } return true; } //use it like this safeGet (document.body, function (o) { o.style.background='black'; }, function (o) { o.style.color='white'; } ); //misuse it like this, but it won't throw an error safeGet ('someIdThatDontExist', function (o) { o.style.background='black'; }, function (o) { o.style.color='white'; } );
You need to login to post a comment.
