Skip to content

Instantly share code, notes, and snippets.

@eikes
eikes / settings.js
Created December 10, 2012 11:49
facetedsearch settings with state object
var settings = {
state : {
"orderBy" : "lastname",
"filters" : {
"category" : ["Bird"],
"continent" : ["Africa"]
}
},
items : example_items,
facets : {
@eikes
eikes / imgpreload.js
Created October 20, 2012 23:23
JavaScript image preloader with callback
/*
* Copyright (C) 2012 Eike Send
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
minwidth(480, null, enhanceMobile, true);
function loadFacebook() {
$script("http//connect.facebook.net/en_US/all.js#xfbml=1");
}
minwidth(600, loadFacebook);
minwidth(480, moveSomething, moveItBack);
var sidebar = document.getElementById("sidebar");
var sidebarElements = document.getElementsByClassName("sidebarElements");
// here is the function call:
relocate(480, sidebarElements, sidebar);
@eikes
eikes / getElementWidth.js
Created April 23, 2012 13:29
Determine html element width without jquery
// Function to determine the width of any element in pixel:
getElementWidth = function (node) {
var ow = node.offsetWidth,
cs = node.currentStyle && node.currentStyle["width"]
|| getComputedStyle && getComputedStyle(node, null).getPropertyValue("width"),
result = parseFloat(ow || cs);
if (cs && cs.match(/%/))
return (parseFloat(cs) / 100) * getElementWidth(node.parentNode);
if (isNaN(result) || result == 0)
return getElementWidth(node.parentNode);
@eikes
eikes / getElementsByClassName.polyfill.js
Created April 4, 2012 08:04
Polyfill for getElementsByClassName
// Add a getElementsByClassName function if the browser doesn't have one
// Limitation: only works with one class name
// Copyright: Eike Send http://eike.se/nd
// License: MIT License
if (!document.getElementsByClassName) {
document.getElementsByClassName = function(search) {
var d = document, elements, pattern, i, results = [];
if (d.querySelectorAll) { // IE8
return d.querySelectorAll("." + search);
String.prototype.toUnicode = function (){
function pad(t) {
return t.length == 4 ? t : pad("0" + t);
}
var r="";
for (var i = 0; i < this.length; i++) {
r += "\\u" + pad(this.charCodeAt(i).toString(16));
}
return r;
}
@eikes
eikes / getTimezone.js
Created February 15, 2012 14:39
Get or estimate browsers timezone name in JavaScript
var d = new Date();
var usertime = d.toLocaleString();
// Some browsers / OSs provide the timezone name in their local string:
var tzsregex = /\b(ACDT|ACST|ACT|ADT|AEDT|AEST|AFT|AKDT|AKST|AMST|AMT|ART|AST|AWDT|AWST|AZOST|AZT|BDT|BIOT|BIT|BOT|BRT|BST|BTT|CAT|CCT|CDT|CEDT|CEST|CET|CHADT|CHAST|CIST|CKT|CLST|CLT|COST|COT|CST|CT|CVT|CXT|CHST|DFT|EAST|EAT|ECT|EDT|EEDT|EEST|EET|EST|FJT|FKST|FKT|GALT|GET|GFT|GILT|GIT|GMT|GST|GYT|HADT|HAEC|HAST|HKT|HMT|HST|ICT|IDT|IRKT|IRST|IST|JST|KRAT|KST|LHST|LINT|MART|MAGT|MDT|MET|MEST|MIT|MSD|MSK|MST|MUT|MYT|NDT|NFT|NPT|NST|NT|NZDT|NZST|OMST|PDT|PETT|PHOT|PKT|PST|RET|SAMT|SAST|SBT|SCT|SGT|SLT|SST|TAHT|THA|UYST|UYT|VET|VLAT|WAT|WEDT|WEST|WET|WST|YAKT|YEKT)\b/gi;
// In other browsers the timezone needs to be estimated based on the offset:
var timezonenames = {"UTC+0":"GMT","UTC+1":"CET","UTC+2":"EET","UTC+3":"EEDT","UTC+3.5":"IRST","UTC+4":"MSD","UTC+4.5":"AFT","UTC+5":"PKT","UTC+5.5":"IST","UTC+6":"BST","UTC+6.5":"MST","UTC+7":"THA","UTC+8":"AWST","UTC+9":"AWDT","UTC+9.5":"ACST"