Skip to content

Instantly share code, notes, and snippets.

@jabranr
Last active December 2, 2015 16:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jabranr/254f6b47dd765ac50654 to your computer and use it in GitHub Desktop.
Save jabranr/254f6b47dd765ac50654 to your computer and use it in GitHub Desktop.
Vanilla JavaScript Bootstrap
/**
* This is small, light weight vanilla JavaScript Bootstrap script.
*
* It comes handy in situations where a framework i.e. jQuery is used only
* for "load" and "ready" events etc. This small script will not only save
* lot of bytes but also gives a basic start on writing object literal JavaScript.
*
* Learn more about object literal JavaScript at following resource:
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Values,_variables,_and_literals
*
* Example demo at: https://gist.github.com/jabranr/7bce230cb1f40580a52c
*
* Inspiration and DOM ready code from http://youmightnotneedjquery.com
*
* MIT License
* @author: Jabran Rafique <hello@jabran.me>
*
*/
!(function(root, doc, factory) {
/**
* Global object to reside inside Window object
*
* Replace JR with any other prefixes and then
* use it accordingly for calling methods from it.
* Prefixes are mostly relevant to the current project.
*/
window.JR = window.JR || factory(root, doc);
})(this, document, function(window, document) {
var jr = {
/**
* Multipurpose function to attach event handlers
*/
on: function(event, element, callback) {
if (document.addEventListener)
element.addEventListener(event, callback, false);
else
element.attachEvent('on' + event, callback);
},
/**
* Method to check if DOM is ready
*/
domReady: function(callback) {
if (document.addEventListener)
document.addEventListener('DOMContentLoaded', callback);
else {
document.attachEvent('onreadystatechange', function() {
if (document.readyState === 'interactive')
callback();
});
}
},
/**
* Method to check if DOM is loaded
*/
domLoaded: function(callback) {
return jr.on('load', window, callback);
}
}
return jr;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment