Skip to content

Instantly share code, notes, and snippets.

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 {
// 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 () {
// Open a new window for external URLs
openNewWindow: function () {
$('.js-external, a[rel="external"], a[href*=".pdf"]').click(function (event) {$(this).attr('href'));
// Initialize function collects all functions meant for every page load
init: function () {
var self = this;
// Call initialize function ONCE
// There is NO NEED to have a dozen disorganized document.ready() functions
jQuery(function () {
'use strict';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment