Skip to content

Instantly share code, notes, and snippets.

@cuth
cuth / noConflictAMD.html
Last active August 29, 2015 14:01
Hack to load modules in a half AMD half browser global environment.
<script>
if (typeof define === 'function' && define.amd) {
window.noConflictAMD = define.amd;
define.amd = null;
}
</script>
<script src="/module.js"></script>
<script>
if (typeof define === 'function') {
define.amd = window.noConflictAMD;
@cuth
cuth / AMDwithManualUMD.md
Last active April 22, 2017 20:47
Mixing an AMD script loading solution with a traditional script loading solution with UMD modules causes a conflict.

Using RequireJS and loading UMD scripts normally

Mixing an AMD script loading solution with a traditional script loading solution with UMD modules causes a conflict.

Two approches to loading scripts

The traditional way to load a script is to manually add a script tag on the page like this:

@cuth
cuth / debug-scroll.md
Last active November 18, 2025 10:13
Find the elements that are causing a horizontal scroll. Based on http://css-tricks.com/findingfixing-unintended-body-overflow/

Debug Horizontal Scroll

(function (d) {
    var w = d.documentElement.offsetWidth,
        t = d.createTreeWalker(d.body, NodeFilter.SHOW_ELEMENT),
        b;
    while (t.nextNode()) {
        b = t.currentNode.getBoundingClientRect();
 if (b.right &gt; w || b.left &lt; 0) {
@cuth
cuth / Stash.md
Last active January 11, 2016 15:16
Cache any jQuery selector with $.stash

jquery.stash

Easily cache a jQuery selector to improve performance when reusing the same selector. Caching is only supported on strings and does not support the context argument.

Poor jQuery example:

$('.select li').html('test');
$('.select li').empty();
@cuth
cuth / LollapaloozaChecklist.md
Created August 5, 2014 20:51
A checklist of things you should bring to Lollapalooza.

Lollapalooza Checklist

  • Lolla bracelets
  • Sun glasses
  • Sunscreen
  • Rain poncho
  • Ziplock bags for phones
  • Hand sanitizer
  • Allergy medication
@cuth
cuth / pre-commit
Last active August 29, 2015 14:05
Run JSHint before a git commit.
#!/bin/sh
#
# Run JSHint validation before commit.
files=$(git diff --cached --name-only --diff-filter=ACMR -- *.js **/*.js)
pass=true
if [ "$files" != "" ]; then
for file in ${files}; do
@cuth
cuth / once.js
Created April 17, 2015 19:30
Run a function once
var once = function (fn) {
'use strict';
var ret;
var called = false;
return function () {
if (called) {
return ret;
}
@cuth
cuth / lazy.js
Last active August 29, 2015 14:19
Run a function if a media query matches or once the media query eventually matches
var lazy = function (mediaQuery, load, unload) {
'use strict';
if (typeof mediaQuery !== 'string') return;
if (typeof load !== 'function') return;
var mql = matchMedia(mediaQuery);
var isLoaded = false;
if (mql.matches) {
@cuth
cuth / README.md
Last active May 20, 2022 22:06
Font Size Bookmarklet

Font Size Bookmarklet

Code

javascript:(function(){var getFontSize=function(el){return parseFloat(getComputedStyle(el,null)['font-size']);};document.addEventListener('wheel',function(e){if(!e.altKey)return;e.preventDefault();var el=e.target;var parent=el.parentElement;var size=getFontSize(el);while(parent&&size===getFontSize(parent)){el.style.fontSize='inherit';el=parent;parent=parent.parentElement;}if(e.wheelDelta>0){size+=1;}else{size-=1;}el.style.fontSize=size+'px';});}());
@cuth
cuth / example.js
Last active January 15, 2016 20:22
Disable AMD when requiring a file.
const safeRequire = require('./safe-require');
const umdStyleMod = safeRequire('umd-style-mod', () => require('umd-style-mod'));