Skip to content

Instantly share code, notes, and snippets.

View dustintheweb's full-sized avatar
🎯
Focusing

Dustin Hoffmann dustintheweb

🎯
Focusing
View GitHub Profile
@dustintheweb
dustintheweb / stock-android-browser-check.js
Created July 8, 2013 22:03
Check if the browser user agent is the stock android browser.
// Stock Android Browser Check >>>>>>>>>>>>>>>>
var ua = navigator.userAgent.toLowerCase();
var isAndroid = ua.indexOf("android") > -1 && ua.indexOf("mobile") && ua.indexOf("chrome")==-1
if(isAndroid) {
// stuff
}
(function ($) {
/**
* @function
* @property {object} jQuery plugin which runs handler function once specified element is inserted into the DOM
* @param {function} handler A function to execute at the time when the element is inserted
* @param {bool} shouldRunHandlerOnce Optional: if true, handler is unbound after its first invocation
* @example $(selector).waitUntilExists(function);
*/
@dustintheweb
dustintheweb / select-infinite-element-multiples-except-first-set.js
Last active December 19, 2015 14:09
Select all element multiples, beyond the ones you want to retain
// >> jQuery - select all element multiples, beyond the ones you want to retain >>>>>>>>>>>>>>>>
$('.someElement').find('ul li+li').hide();
// li+li+li
// li+li+li+li, etc
// same syntax as css
@dustintheweb
dustintheweb / ternary-conditional.js
Created July 18, 2013 15:50
Ternary (Conditional) Operator and Style. Useful for rendering an inline if statement
// Psuedo:
(some_condition) ? do_something_simple : do_something_else;
// Example within an API
$('.featured-carousel .carousel').carouFredSel({
scroll: {
fx: ($('html').hasClass('ie8'))?'scroll':'crossfade'
}
});
@dustintheweb
dustintheweb / javascript-fundamentals-oft-forgotten.js
Last active December 21, 2015 03:09
Javascript Fundamentals Oft Forgotten
/* ##### General Tips & Syntax ##### */
// scope
var area = 36; // a var with global scope
var volume = function (w, l, h) {
var depth = 15; // a var with local scope
area = w * l; // a var with global scope
};
// line wrap
@dustintheweb
dustintheweb / delegate-event-syntax.js
Created August 20, 2013 20:41
Delegate Event Syntax / Example
$services.on('click','.sub-columns a',function(){
// stuff
});
$someVariable.on('someevent','.classtarget',function(){
// stuff
});
// used for targeting specific events & classes
// more optimal than a dom crawl
@dustintheweb
dustintheweb / jquery-toggle-replacement-syntax.js
Last active December 21, 2015 10:09
jQuery Replacement Syntax for Depreciated Toggle Function
$('#element').click(function() {
if ($(this).data('clicked')) {
$(this).css({'display': 'block'});
$(this).data('clicked', false);
} else {
$(this).css({'display': 'none'});
$(this).data('clicked', true);
}
});
@dustintheweb
dustintheweb / inject-url-suffix-to-object.js
Created August 22, 2013 17:47
Inject the last part of the current page url into an object (class, id, etc)
var url = document.referrer,
urlsplit = url.split("/").pop();
$('body').addClass(urlsplit);
@dustintheweb
dustintheweb / for-loop-syntax.js
Created August 28, 2013 11:22
various examples of for loops & syntax
for (var i=1; i<26; i = i++) { // counting from 1 to 25
console.log(i);
}
for (var i = 5; i < 51; i+=5) { // counting from 5 to 50 in increments of 5
console.log(i);
}
for (var i=8; i<120; i+=12) { // counting from 8 to 119 in increments of 12
console.log(i);
@dustintheweb
dustintheweb / loop-through-array.js
Created August 28, 2013 11:55
Parse through an array with a loop & print out its contents
var cities = ["Melbourne", "Amman", "Helsinki", "NYC", "Tulsa", "Nome"];
for (var i = 0; i < cities.length; i++) {
console.log("I would like to visit " + cities[i]);
}