Skip to content

Instantly share code, notes, and snippets.

HTML fallback content

Wouldn't it have been nice if HTML had fully embraced the approach it takes for iframe and object tags by having fallback content for other elements.

Images could have not only alt text, but also fallback images.

<img src="banana.png">
  A very tasty banana
@cjwainwright
cjwainwright / 1.js
Last active December 14, 2015 02:49 — forked from padolsey/1.js
/x/==x
@cjwainwright
cjwainwright / withDefaults.js
Created January 30, 2013 23:28
A simple method of creating functions with default arguments.
Function.prototype.withDefaults = function () {
var defaults = [].slice.call(arguments);
var f = this;
return function () {
arguments.__proto__ = defaults;
arguments.length = f.length;
return f.apply(this, arguments);
};
};
@cjwainwright
cjwainwright / event.js
Created December 13, 2012 00:30
A simple event maker. Creates a function to which other functions can be added and removed for execution when the outer function is called.
function event() {
var a = [].slice.call(arguments);
var fn = function() {
var scope = this;
var args = [].slice.call(arguments);
a.forEach(function(f){f.apply(scope, args);});
};
fn.add = function() {
a.push.apply(a, arguments);
return fn;
@cjwainwright
cjwainwright / make.js
Created December 1, 2012 15:21
A function to create a new object using a constructor function when you only have an array of arguments, analogous to how you would use "apply" to call a function, but for "new".
// A function to create a new object using a constructor function when you only
// have an array of arguments, analogous to how you would use "apply" to call a
// function, but for "new".
function make(C, args) {
function T(){
C.apply(this, args);
}
T.prototype = C.prototype;
return new T();
@cjwainwright
cjwainwright / selector
Created November 18, 2012 21:15
Simple DOM selector for modern browsers / single line jQuery replacement!
function $(selector) {
return Array.apply(null, document.querySelectorAll(selector));
}
@cjwainwright
cjwainwright / innerHTMLissue.html
Last active August 29, 2015 14:06
Demonstrates how innerHTML can destroy the DOM structure of the original child nodes it replaces in IE9
<!doctype html>
<html>
<body>
<div id="a">
<div id="b">
<div id="c">Original Child 1</div>
<div>Original Child 2</div>
</div>
</div>
<script type="text/javascript">
@cjwainwright
cjwainwright / proxyNamespace.js
Last active August 29, 2015 14:01
Using ES6 Proxies to create a namespace root allowing automatically created child namespaces via a get trap
function ns(x) {
return new Proxy(x, {
get: function(target, name) {
if(target.hasOwnProperty(name)) {
return target[name];
}
return target[name] = ns({});
}
});
}