Skip to content

Instantly share code, notes, and snippets.

@vorporeal
Last active December 11, 2015 10:38
Show Gist options
  • Save vorporeal/4588364 to your computer and use it in GitHub Desktop.
Save vorporeal/4588364 to your computer and use it in GitHub Desktop.
function browserVersionOf(unprefixedName, parentObject) {
// parentObject is optional and usually omitted, set it to window if it's not specified.
parentObject = parentObject || window;
var baseName = unprefixedName.charAt(0).toUpperCase() + unprefixedName.substring(1);
var i = 0, variants = [unprefixedName, 'webkit', 'moz', 'ms', 'o'];
var fullName = variants[0];
do {
if (typeof parentObject[fullName] == 'function') {
return parentObject[fullName];
}
fullName = variants[++i] + baseName;
} while (variants[i]);
throw "Browser lacks support for " + unprefixedName;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment