Skip to content

Instantly share code, notes, and snippets.

@WouterSpaak
Last active August 29, 2015 13:56
Show Gist options
  • Save WouterSpaak/9131926 to your computer and use it in GitHub Desktop.
Save WouterSpaak/9131926 to your computer and use it in GitHub Desktop.
Handige Helperfuncties voor Onlineredacteuren van De Gelderlander.
/**
* Element.hasClass(className)
*
* Extends the Element prototype to query for a certain class.
*
* @param string className
* String to query for classes
* @return boolean
* Returns true if class is found, else returns false
*
* @author Wouter Spaak
*
* @example
* <div id="baz" class="foo bar"></div>
* var elm = document.getElementById('baz');
* elm.hasClass('foo'); returns true
* elm.hasClass('bae'); returns false
*/
Element.prototype.hasClass = function(className) {
if(this.classList) {
return this.classList.contains(className);
}
else {
return new Regexp('(^| )' + className + '( |$)', 'gi').test(this.className);
}
}
/**
* document.ready(fn)
*
* Extends the document prototype to call a function when the document
* DOM-structure is fully loaded.
*
* @param function fn
* Function to call when the document is ready
* @return void
*
* @author Wouter Spaak
*
* @example
* document.ready(foo());
*
* var foo = function() {
* console.log('baz');
* }
* // Logs 'baz' to the console when the DOM is ready.
*/
Document.prototype.ready = function(fn) {
if(this.addEventListener) {
this.addEventListener('DOMContentLoaded', fn);
}
else {
this.attachEvent('onreadystatechange', function() {
if(this.readyState === 'interactive') {
fn();
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment