Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// One global variable for all your JavaScript
var MY_SITE = window.MY_SITE || {};
// Requires jQuery
// Global jQuery object is aliased to avoid polluting global namespace with "$"
MY_SITE = (function ($) {
'use strict';
var private_function;
// Private function
// Can only be called by public functions below because of closure
private_function = function () {
return;
};
return {
// Public function
// Can be called "outside" of this closure on an individual basis
// e.g. MY_SITE.public_function()
// Or can be bundled in single function meant specifically for page load
// e.g. MY_SITE.init()
public_function: function () {
private_function();
return;
},
// Open a new window for external URLs
openNewWindow: function () {
$('.js-external, a[rel="external"], a[href*=".pdf"]').click(function (event) {
window.open($(this).attr('href'));
event.preventDefault();
});
},
// Initialize function collects all functions meant for every page load
init: function () {
var self = this;
self.openNewWindow();
}
};
}(jQuery));
// Call initialize function ONCE
// There is NO NEED to have a dozen disorganized document.ready() functions
jQuery(function () {
'use strict';
MY_SITE.init();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment