Let's say I want to detect support for notifications (http://notifications.spec.whatwg.org/) in a JavaScript library.
I can use window.Notification !== undefined
. But what if the users of the library (or another library) also have some kind of global Notification
object defined for completely different purpose?
On the other hand, what if the other library is a polyfill? Then their Notification
object is acceptable. Should I test for all methods in addition to testing for the global object?
Update:
I have noticed an interesting thing in one notifications polyfill:
ret[toString] = function() {
return 'function Notification() { [native code] }';
};
How reliable is relying on something like that to detect whether it is a native/polyfill object?
So essentially there are two choices.
First is to try and rely on 'function Notification() { [native code] }'
toString
value, as it is being set by some polyfills at least. Unfortunately, this not reliable, as I found no information on whether it is a common approach within polyfill authors and whether it is a reliable return value in browsers.
Another option, as suggested in comments, is to ignore potential conflicts and just go forward just testing for existence. That's what I went with for now.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With