Skip to content

Instantly share code, notes, and snippets.

@replete
replete / gist:2384817
Created April 14, 2012 14:36
Detect IE version 5-9 (based on conditional comments)
/* Don't forget, the currently unreleased IE10 is supposedly dropping support for conditional comments
so we'll need something else in this function to detect IE10 (hopefully, we'll never need to) */
var ie = (function(){
var undef,
v = 3,
div = document.createElement('div'),
all = div.getElementsByTagName('i');
while (
div.innerHTML = '<!--[if gt IE ' + (++v) + ']><i></i><![endif]-->',
@replete
replete / gist:2384841
Created April 14, 2012 14:39
Get elements by class name - getElementsByClassName
/* Example usage:
var widgets = getElementsByClassName(document, "a-css-classname");
for (var i = 0; i < widgets.length; i++) {
//Do something with widgets[i]
}
*/
function getElementsByClassName(node,classname) {
if (node.getElementsByClassName) {
return node.getElementsByClassName(classname);
@replete
replete / gist:2384895
Created April 14, 2012 14:47
Insert UA/platform strings into html data attribute
/*
!! DANGER DANGER WILL ROBINSON
*/
document.documentElement.setAttribute('data-ua',navigator.userAgent);
document.documentElement.setAttribute('data-platform',navigator.platform);
@replete
replete / gist:2384908
Created April 14, 2012 14:49
Check domain before running domain-specific code, e.g. analytics
if (document.location.hostname.indexOf("mydomain.com") > -1) {
//GA: Event Tracking, for example
$("a[href$=pdf]").click(function () {
var name = $(this).attr("title") || "";
_gaq.push(['_trackEvent', 'PDF', 'Download', name]);
});
//etc
@replete
replete / gist:2384974
Created April 14, 2012 14:53
Once-only sequential 'animation' (css classes) of element children on page load
/*
After the page loads, highlight important parts on the page (e.g. navigation),
by sequentially applying a css class to each, once.
Styles/animation handled by CSS.
*/
var $delayHoverAnims = $("[data-delay-hover-anim]");
@replete
replete / gist:2384987
Created April 14, 2012 14:55
Change <body> background url through data-attribute
/* -------------------------------------- */
//Body background
var $bodyBackground = $("[data-body-background]:eq(0)"),
backgroundURL = $bodyBackground.attr("data-body-background");
if ($bodyBackground.length > 0) {
$body
.css("background", "url(" + backgroundURL + ") no-repeat top center #000");
}
@replete
replete / gist:2384996
Created April 14, 2012 14:56
Simple rotating content children
/* -------------------------------------- */
//Simple Rotator
var $rotatorContainers = $("[data-rotator]");
$rotatorContainers.each(function () {
var $container = $(this),
$items = $container.children($container.attr("data-rotator")),
itemTotal = $items.length - 1,
autoplay = $container.attr("data-rotator-autoplay");
@replete
replete / gist:2385033
Created April 14, 2012 15:01
Simple content panes with navigation
/* -------------------------------------- */
//Content panes with navigation
var $panesContainers = $("[data-panes]");
$panesContainers.each(function () {
var $container = $(this),
$items = $container.children($container.attr("data-panes")),
itemTotal = $items.length - 1,
$navigation = $container.find($container.attr("data-panes-navigation"));
@replete
replete / gist:2385046
Created April 14, 2012 15:02
iOS like scrollbars (requires nicescroll)
/* -------------------------------------- */
//iOS-like scrollbar
var $niceScrollContainers = $("[data-ios-scroll]");
$niceScrollContainers.each(function () {
var $container = $(this),
scrollClass = $container.attr("data-ios-scroll"),
cursorWidth = "6px",
$contentsLastChild = $container.children("*:last-child");
@replete
replete / gist:2385061
Created April 14, 2012 15:04
Text entry character count and truncate contenteditable
/* -------------------------------------- */
//Char count & truncate
var $charCount = $("[data-char-count]");
$charCount.each(function () {
var $this = $(this),
$charSelector = $($this.attr("data-char-count")),
charMax = $this.attr("data-char-count-max") || false;