Some simple stuff that I use very often in projects.
Last active
December 17, 2015 11:09
-
-
Save nessthehero/5599884 to your computer and use it in GitHub Desktop.
Utility belt of awesome, small javascript snippets that I use very often.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Avoid `console` errors in browsers that lack a console. | |
(function() { | |
var method; | |
var noop = function () {}; | |
var methods = [ | |
'assert', 'clear', 'count', 'debug', 'dir', 'dirxml', 'error', | |
'exception', 'group', 'groupCollapsed', 'groupEnd', 'info', 'log', | |
'markTimeline', 'profile', 'profileEnd', 'table', 'time', 'timeEnd', | |
'timeStamp', 'trace', 'warn' | |
]; | |
var length = methods.length; | |
var console = (window.console = window.console || {}); | |
while (length--) { | |
method = methods[length]; | |
// Only stub undefined methods. | |
if (!console[method]) { | |
console[method] = noop; | |
} | |
} | |
}()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Returns a function, that, as long as it continues to be invoked, will not | |
// be triggered. The function will be called after it stops being called for | |
// N milliseconds. If `immediate` is passed, trigger the function on the | |
// leading edge, instead of the trailing. | |
function debounce(func, wait, immediate) { | |
var timeout; | |
return function() { | |
var context = this, args = arguments; | |
var later = function() { | |
timeout = null; | |
if (!immediate) func.apply(context, args); | |
}; | |
var callNow = immediate && !timeout; | |
clearTimeout(timeout); | |
timeout = setTimeout(later, wait); | |
if (callNow) func.apply(context, args); | |
}; | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Images loaded | |
(function(c,q){var m="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==";c.fn.imagesLoaded=function(f){function n(){var b=c(j),a=c(h);d&&(h.length?d.reject(e,b,a):d.resolve(e));c.isFunction(f)&&f.call(g,e,b,a)}function p(b){k(b.target,"error"===b.type)}function k(b,a){b.src===m||-1!==c.inArray(b,l)||(l.push(b),a?h.push(b):j.push(b),c.data(b,"imagesLoaded",{isBroken:a,src:b.src}),r&&d.notifyWith(c(b),[a,e,c(j),c(h)]),e.length===l.length&&(setTimeout(n),e.unbind(".imagesLoaded", | |
p)))}var g=this,d=c.isFunction(c.Deferred)?c.Deferred():0,r=c.isFunction(d.notify),e=g.find("img").add(g.filter("img")),l=[],j=[],h=[];c.isPlainObject(f)&&c.each(f,function(b,a){if("callback"===b)f=a;else if(d)d[b](a)});e.length?e.bind("load.imagesLoaded error.imagesLoaded",p).each(function(b,a){var d=a.src,e=c.data(a,"imagesLoaded");if(e&&e.src===d)k(a,e.isBroken);else if(a.complete&&a.naturalWidth!==q)k(a,0===a.naturalWidth||0===a.naturalHeight);else if(a.readyState||a.complete)a.src=m,a.src=d}): | |
n();return d?d.promise(g):g}})(jQuery); | |
// Images loaded, but also does background images | |
var images = $('img, .bg-img'); | |
$.each(images, function(){ | |
var el = $(this), | |
image = el.css('background-image').replace(/"/g, '').replace(/url\(|\)$/ig, ''); | |
if(image && image !== '' && image !== 'none') | |
images = images.add($('<img>').attr('src', image)); | |
if(el.is('img')) | |
images = images.add(el); | |
}); | |
images.imagesLoaded(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* IndexOf polyfill */ | |
if (!Array.prototype.indexOf) { | |
Array.prototype.indexOf = function (searchElement, fromIndex) { | |
if ( this === undefined || this === null ) { | |
throw new TypeError( '"this" is null or not defined' ); | |
} | |
var length = this.length >>> 0; // Hack to convert object.length to a UInt32 | |
fromIndex = +fromIndex || 0; | |
if (Math.abs(fromIndex) === Infinity) { | |
fromIndex = 0; | |
} | |
if (fromIndex < 0) { | |
fromIndex += length; | |
if (fromIndex < 0) { | |
fromIndex = 0; | |
} | |
} | |
for (;fromIndex < length; fromIndex++) { | |
if (this[fromIndex] === searchElement) { | |
return fromIndex; | |
} | |
} | |
return -1; | |
}; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Pad numbers | |
function pad(input) { if (input < 10) {return '0' + input;} return input; } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Placeholder polyfill | |
if (!Modernizr.input.placeholder) { | |
$('input[placeholder]:not(.ph), textarea[placeholder]:not(.ph)').each(function () { | |
var place = $(this).attr('placeholder'); | |
$(this).attr('value', place); | |
$(this).bind('focus', function () { | |
if ($.trim($(this).attr('value')) === place) { | |
$(this).attr('value', ''); | |
} | |
}); | |
$(this).bind('blur', function () { | |
if ($.trim($(this).attr('value')) === '') { | |
$(this).attr('value', place); | |
} | |
}); | |
$(this).addClass('ph'); | |
}); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function q() { | |
var vars = [], | |
grabUrl = window.location.search, | |
parts, | |
pieces, | |
qs = '', | |
qsVals = []; | |
if (typeof arguments[0] === 'string') { | |
qs = arguments[0]; | |
} | |
if (typeof grabUrl !== 'undefined' && grabUrl !== '') { | |
grabUrl = grabUrl.replace('?', ''); | |
parts = grabUrl.split('&'); | |
for (var j in parts) { | |
if (parts.hasOwnProperty(j)) { | |
pieces = parts[j].split('='); | |
if (vars.length !== 0) { | |
for (var i in vars) { | |
if (vars.hasOwnProperty(i)) { | |
if (vars[i].name === pieces[0].toString()) { | |
vars[i].values.push(pieces[1]); | |
} else { | |
vars.push({ 'name' : pieces[0].toString(), 'values' : [pieces[1]] }); | |
} | |
} | |
} | |
} else { | |
vars.push({ 'name' : pieces[0].toString(), 'values' : [pieces[1]] }); | |
} | |
} | |
} | |
if (qs !== '') { | |
for (var b in vars) { | |
if (vars.hasOwnProperty(b)) { | |
if (vars[b].name === qs) { | |
return vars[b].values; | |
} | |
} | |
} | |
return ['-1']; | |
} | |
return vars; | |
} else { | |
return []; | |
} | |
} | |
function h() { | |
var vars = [], | |
grabUrl = window.location.hash, | |
parts, | |
pieces, | |
qs = '', | |
qsVals = []; | |
if (typeof arguments[0] === 'string') { | |
qs = arguments[0]; | |
} | |
if (typeof grabUrl !== 'undefined' && grabUrl !== '') { | |
grabUrl = grabUrl.replace('#', ''); | |
parts = grabUrl.split('&'); | |
for (var j in parts) { | |
if (parts.hasOwnProperty(j)) { | |
pieces = parts[j].split('='); | |
if (vars.length !== 0) { | |
for (var i in vars) { | |
if (vars.hasOwnProperty(i)) { | |
if (vars[i].name === pieces[0].toString()) { | |
vars[i].values.push(pieces[1]); | |
} else { | |
vars.push({ 'name' : pieces[0].toString(), 'values' : [pieces[1]] }); | |
} | |
} | |
} | |
} else { | |
vars.push({ 'name' : pieces[0].toString(), 'values' : [pieces[1]] }); | |
} | |
} | |
} | |
if (qs !== '') { | |
for (var b in vars) { | |
if (vars.hasOwnProperty(b)) { | |
if (vars[b].name === qs) { | |
return vars[b].values; | |
} | |
} | |
} | |
return ['-1']; | |
} | |
return vars; | |
} else { | |
return []; | |
} | |
} | |
// | |
function qU(url) { | |
var vars = [], | |
grabUrl = url, | |
parts, | |
pieces, | |
qs = '', | |
qsVals = []; | |
if (typeof arguments[1] === 'string') { | |
qs = arguments[1]; | |
} | |
if (typeof grabUrl !== 'undefined' && grabUrl !== '') { | |
grabUrl = grabUrl.split('?'); | |
parts = grabUrl[1].split('&'); | |
for (var j in parts) { | |
if (parts.hasOwnProperty(j)) { | |
pieces = parts[j].split('='); | |
if (vars.length !== 0) { | |
for (var i in vars) { | |
if (vars[i].name === pieces[0].toString()) { | |
vars[i].values.push(pieces[1]); | |
} else { | |
vars.push({ 'name' : pieces[0].toString(), 'values' : [pieces[1]] }); | |
} | |
} | |
} else { | |
vars.push({ 'name' : pieces[0].toString(), 'values' : [pieces[1]] }); | |
} | |
} | |
} | |
if (qs !== '') { | |
for (var b in vars) { | |
if (vars.hasOwnProperty(b)) { | |
if (vars[b].name === qs) { | |
return vars[b].values; | |
} | |
} | |
} | |
return ['-1']; | |
} | |
return vars; | |
} else { | |
return []; | |
} | |
} | |
function hU(url) { | |
var vars = [], | |
grabUrl = url, | |
parts, | |
pieces, | |
qs = '', | |
qsVals = []; | |
if (typeof arguments[1] === 'string') { | |
qs = arguments[1]; | |
} | |
if (typeof grabUrl !== 'undefined' && grabUrl !== '') { | |
grabUrl = grabUrl.split('#'); | |
parts = grabUrl[1].split('&'); | |
for (var j in parts) { | |
if (parts.hasOwnProperty(j)) { | |
pieces = parts[j].split('='); | |
if (vars.length !== 0) { | |
for (var i in vars) { | |
if (vars[i].name === pieces[0].toString()) { | |
vars[i].values.push(pieces[1]); | |
} else { | |
vars.push({ 'name' : pieces[0].toString(), 'values' : [pieces[1]] }); | |
} | |
} | |
} else { | |
vars.push({ 'name' : pieces[0].toString(), 'values' : [pieces[1]] }); | |
} | |
} | |
} | |
if (qs !== '') { | |
for (var b in vars) { | |
if (vars.hasOwnProperty(b)) { | |
if (vars[b].name === qs) { | |
return vars[b].values; | |
} | |
} | |
} | |
return ['-1']; | |
} | |
return vars; | |
} else { | |
return []; | |
} | |
} | |
// q(); | |
// h(); | |
// qU('http://catalog.normandale.edu/585.htm?iframe=true&width=720&height=500'); | |
// hU('http://normandale-devdss.ingeniuxondemand.com/communication/communication-course-track#lol=yes&fail=test&fail=no'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* jQuery-syncHeight - v1.5.0 | |
* https://github.com/ginader/syncHeight | |
* Copyright (c) 2013 Dirk Ginader; | |
* Dual licensed under the MIT and GPL licenses: | |
* http://www.opensource.org/licenses/mit-license.php | |
* http://www.gnu.org/licenses/gpl.html */ | |
(function(e){var t=function(){var e=0,t=[["min-height","0px"],["height","1%"]],n=/(msie) ([\w.]+)/.exec(navigator.userAgent.toLowerCase())||[],i=n[1]||"",h=n[2]||"0";return"msie"===i&&7>h&&(e=1),{name:t[e][0],autoheightVal:t[e][1]}};e.getSyncedHeight=function(n){var i=0,h=t();return e(n).each(function(){e(this).css(h.name,h.autoheightVal);var t=parseInt(e(this).css("height"),10);t>i&&(i=t)}),i},e.fn.syncHeight=function(n){var i={updateOnResize:!1,height:!1},h=e.extend(i,n),s=this,a=0,c=t().name;return a="number"==typeof h.height?h.height:e.getSyncedHeight(this),e(this).each(function(){e(this).css(c,a+"px")}),h.updateOnResize===!0&&e(window).resize(function(){e(s).syncHeight()}),this},e.fn.unSyncHeight=function(){var n=t().name;e(this).each(function(){e(this).css(n,"")})}})(jQuery); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Tweet sized templating | |
function t(s, d) { | |
var p; | |
for (p in d) { | |
if (d.hasOwnProperty(p)) { | |
s = s.replace(new RegExp('{' + p + '}', 'g'), d[p]); | |
} | |
} | |
return s; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment