« Back to Index

JavaScript function that lets you query for the API or provide a fallback if not available

View original Gist on GitHub

find-or-fallback.js

function findOrFallback(where, what, fallback) {
  for(var
    vendors = ['', 'webkit', 'moz', 'ms', 'o'],
    first = what.charAt(0),
    others = first.toUpperCase(),
    suffix = what.slice(1),
    i = 0, length = vendors.length,
    current;
    i < length; i++
  ) {
    current = where[vendors[i] + (i ? others : first) + suffix];
    if (current) return current;
  }
  return fallback;
}