Skip to content

Instantly share code, notes, and snippets.

@alexbaulch
alexbaulch / umd.js
Last active August 29, 2015 14:05
AMDify and CommonJSify your jQuery plugin! After spending ages trying to get a decent way to use jQuery plugins with Broswerify or RequireJS I found the following in Addy Osmani's UMD repo (https://github.com/umdjs/umd/blob/master/jqueryPluginCommonjs.js), completely hijacked, none of my own thinking or doing here but needed a way of finding thi…
// Uses CommonJS, AMD or browser globals to create a jQuery plugin.
(function (factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['jquery'], factory);
} else if (typeof exports === 'object') {
// Node/CommonJS
factory(require('jquery'));
} else {
@alexbaulch
alexbaulch / debounce.js
Last active December 12, 2017 15:42
Vanilla JS Debounce CommonJS Module
/**
@module debounce
@param {function} func the function to be debounced
@param {int} wait the time to wait after event has stopped firing
@param {boolean} immediate whether to run the function immediately or wait for the timeout
*/
module.exports = function(func, wait, immediate) {
// create var to store the timeout in
var timeout;
@alexbaulch
alexbaulch / clearfix.scss
Last active August 29, 2015 14:06
Sass clearfix mixin
@mixin clearfix {
&:after {
clear:both;
content:"";
display:table;
}
}
@alexbaulch
alexbaulch / cookie.js
Created September 25, 2014 13:45
CommonJS cookie handler
@alexbaulch
alexbaulch / get-query-variable.js
Last active August 29, 2015 14:07
A small CommonJS module for fetching the value of a given url param.
module.exports = function(variable) {
//get query params and remove the beginning '?'
var query = window.location.search.substring(1);
//split on ampersand
var vars = query.split('&');
//loop through 'vars' array
for (var i = 0; i < vars.length; i++) {
// split on '=' to give key value pairs
@alexbaulch
alexbaulch / index.html
Last active August 29, 2015 14:10
adding IE specific classes to the html tag to enable styling based on just specific versions of IE
<!--[if lt IE 7]> <html class="lt-ie10 lt-ie9 lt-ie8 lt-ie7" lang="en"> <![endif]-->
<!--[if IE 7]> <html class="lt-ie10 lt-ie9 lt-ie8" lang="en"> <![endif]-->
<!--[if IE 8]> <html class="lt-ie10 lt-ie9" lang="en"> <![endif]-->
<!--[if IE 9]> <html class="lt-ie10" lang="en"> <![endif]-->
<!--[if gt IE 9]><!-->
<html class="" lang="en">
<!--<![endif]-->
@alexbaulch
alexbaulch / ie8-safe-svg.html
Last active August 29, 2015 14:10
Wrapper for safe use of SVGs in IE8
<!--[if gte IE 9]><!-->
<svg>Goes here</svg>
<!--<![endif]-->
<!--[if lte IE 8]>
Fallback image goes here for IE8
<img src="http://placehold.it/300x300" alt="Fallback image" />
<![endif]-->
@alexbaulch
alexbaulch / _placeholder-mixin.scss
Created December 1, 2014 00:22
Sass (scss) mixin for styling placeholders cross browser
@mixin placeholder {
::-webkit-input-placeholder {@content}
:-moz-placeholder {@content}
::-moz-placeholder {@content}
:-ms-input-placeholder {@content}
}
@alexbaulch
alexbaulch / google-analytics.js
Created December 2, 2014 17:03
Check form to which fields have been filled in before a user abandons it and send the fields to Google Analytics.
// stuff to do before the page un loads
if ( $('[data-page-name="contact"]').length ) {
var count = 0;
window.onbeforeunload = function(e) {
var contactUsForm = $('[data-page-name="contact"]').find('.contact-us-form');
var requiredFields = contactUsForm.find('[data-val-required]');
var completedFields = [];
var missedFields = [];
@alexbaulch
alexbaulch / pub-sub.js
Created December 11, 2014 16:42
Miniature PubSub library in AMD pattern
define(function() {
'use strict';
var pubSub = function pubSubFn() {
var topics = {};
function subscribe(topic, listener) {
// Create the topic's object if not yet created
if (!topics[topic]) {
topics[topic] = {