Skip to navigation

[JS] HOWTO: Monkey patch IE for support for

Written by Matthew Scharley at Friday, 09 March 2012 - 01:00

Last updated: Friday, 08 June 2012 - 22:49

One of the better ways of detecting an object’s type name in Javascript is It’s also the way that the Drupal states API does it. Unfortunately, Internet Explorer doesn’t support the non-standardised, though rather standard property.

So, here’s how to monkey patch it into IE9 and above. IE8 doesn’t support defineProperty properly and earlier versions don’t support it at all so those versions are still unsupported by this method.

 * Hack in support for for browsers that don't support it.
 * IE, I'm looking at you.
if ( === undefined && Object.defineProperty !== undefined) {
    Object.defineProperty(Function.prototype, 'name', {
        get: function() {
            var funcNameRegex = /function\s([^(]{1,})\(/;
            var results = (funcNameRegex).exec((this).toString());
            return (results && results.length > 1) ? results[1].trim() : "";
        set: function(value) {}
blog comments powered by Disqus