Detect if function is native to browser

javascript native code
function tostring() native code
function () { [native code] } vue
function link() { [native code] }

I am trying to iterate over all the globals defined in a website, but in doing so I am also getting the native browser functions.

var numf=0; var nump=0; var numo=0; 
for(var p in this) { 
    if(typeof(this[p]) === "function"){
    } else if(typeof p != 'undefined'){
    } else { 

Is there a way to determine if a function is native to the browser or created in a script?

You can call the inherited .toString() function on the methods and check the outcome. Native methods will have a block like [native code].

if( this[p].toString().indexOf('[native code]') > -1 ) {
    // yep, native in the browser

Update because a lot of commentators want some clarification and people really have a requirement for such a detection. To make this check really save, we should probably use a line line this:

if( /\{\s+\[native code\]/.test( this[ p ] ) ) ) {
    // yep, native

Now we're using the .toString method from the prototype of Function which makes it very unlikely if not impossible some other script has overwritten the toString method. Secondly we're checking with a regular expression so we can't get fooled by comments within the function body.

javascript - Detect if function is native to browser, I am trying to iterate over all the globals defined in a website, but in doing so I am also getting the native browser functions. var numf=0; var nump=0; var numo=0  Detect if a Function is Native Code with JavaScript. Every once a while I'll test is a given function is native code -- it's an important part of feature testing whether a function was provided by the browser or via a third party shim which acts like the native feature.

function isFuncNative(f) {
       return !!f && (typeof f).toLowerCase() == 'function' 
       && (f === Function.prototype 
       || /^\s*function\s*(\b[a-z$_][a-z0-9$_]*\b)*\s*\((|([a-z$_][a-z0-9$_]*)(\s*,[a-z$_][a-z0-9$_]*)*)\)\s*{\s*\[native code\]\s*}\s*$/i.test(String(f)));

this should be good enough. this function does the following tests:

  1. null or undefined;
  2. the param is actually a function;
  3. the param is Function.prototype itself (this is a special case, where Function.prototype.toString gives function Empty(){})
  4. the function body is exactly function <valid_function_name> (<valid_param_list>) { [native code] }

the regex is a little bit complicated, but it actually runs pretty decently fast in chrome on my 4GB lenovo laptop (duo core):

var n = (new Date).getTime(); 
for (var i = 0; i < 1000000; i++) {
    i%2 ? isFuncNative(isFuncNative) : 
(new Date).getTime() - n;

3023ms. so the function takes somewhere around 3 micro-sec to run once all is JIT'ed.

It works in all browsers. Previously, I used, this crashes IE, since in IE, the DOM element methods and window methods are NOT functions, but objects, and they don't have toString method. String constructor solves the problem elegantly.

Detect if a Function is Native with JavaScript, testing whether a function was provided by the browser or via a third party shim which acts like the native feature. The best way to detect this,  Browser detection on Windows can't be done right. This is the try-everything-and-fail-silently approach. It accounts for architecture differences, normalizes environment variables, tries default locations, searches the registry (in the HKLM and HKCU hives as well as WoW counterparts) (including Start Menu Internet Applications , Google Updater and more), and looks in PATH .

Function.prototype.toString can be spoofed, something kinda like this:

Function.prototype.toString = (function(_toString){
  return function() {
    if (shouldSpoof) return 'function() { [native code] }'
    return _toString.apply(this, arguments)

You can detect if Function.prototype.toString is vandalized by trapping .apply(), .call(), .bind() (and others).

And if it was, you can grab a "clean" version of Function.prototype.toString from a newly injected IFRAME.

Detecting built-in host methods, detect native method in object // not same scope of isHostObject var isNative W3C browser return "[native code]" // Safari <= 2.0.4 will return "[function]" If you look closely, isNative actually doesn't test whether method is  IOS WKWebview — An object that displays interactive web content, such as for an in-app browser. It was introduced in IOS 8 and is the new standard WebView (it is the default for IOS when using react-native-webview). IOS UIWebview — A view that embeds web content in your app.

I tried a different approach. This is only tested for firefox, and chrome.

function isNative(obj){
    //Is there a function?
    //You may throw an exception instead if you want only functions to get in here.

    if(typeof obj === 'function'){
        //Check does this prototype appear as an object?
        //Most natives will not have a prototype of [object Object]
        //If not an [object Object] just skip to true.
        if( === '[object Object]'){
            //Prototype was an object, but is the function Object?
            //If it's not Object it is not native.
            //This only fails if the Object function is assigned to prototype.constructor, or
            //Object function is assigned to the prototype, but
            //why you wanna do that?
            if(String(obj.prototype.constructor) !== String(Object.prototype.constructor)){
                return false;
    return true;

function bla(){}

isNative(bla); //false
isNative(Number); //true
isNative(Object); //true
isNative(Function); //true
isNative(RegExp); //true

Function.prototype.toSource(), Although it may still work in some browsers, its use is discouraged since it could be removed at any time. function Function() { [native code] }; For custom functions, toSource() returns the JavaScript source that defines the object as a string. If you'd like to contribute to the data, please check out  Modernizr for feature detection performs 3 basic functions:-. Adds classes indicating feature support which can be used to conditionally applying CSS styling rules to different elements. Creates a javascript object to check or validate support for any HTML or CSS feature in a browser.

almost all of these will fail, because:

function notNative(){}
notNative.toString = String.bind(0, "function notNative() { [native code] }");
console.log( notNative.toString() );

Browser detection using the user agent, The first method uses horizontal Flexboxes to group the content such that when the page is displayed to the end user, all the dogs boxes are at  The window.navigator object contains information about the visitor's browser. The window.navigator object can be written without the window prefix. The cookieEnabled property returns true if cookies are enabled, otherwise false: Strange enough, "Netscape" is the application name for both IE11, Chrome, Firefox, and Safari.

Detecting HTML5 Features, This method will only exist if your browser supports the canvas API . else { // no native video support available :( // maybe check for QuickTime or Flash instead }. I have looked around a lot, and I understand that there are a lot of ways to detect internet explorer. My problem is this: I have an area on my HTML doc, that when clicked, calls a javascript function that is incompatible with internet explorer of any kind. I wish to detect if IE is being used, and if so, set a variable as true.

JavaScript isArray() Function, Check whether an object is an array: function myFunction() { var fruits = ["Banana​", "Orange", "Apple", "Mango"]; var x = document.getElementById("demo"); x. Modernizr runs automatically. There is no modernizr_init() function to call. When it runs, it creates a global object called Modernizr, that contains a set of Boolean properties for each feature it can detect. For example, if your browser supports the canvas API, the Modernizr.canvas property will be true.

Check if Event was Triggered or Native, $('button').on('click', function (event) { if (e.originalEvent Email *. Website. Save my name, email, and website in this browser for the next time I comment. In case that your device isn't supported, add a comment and we will try to fix it and update the function. List of devices tested and confirmed working: Safari, Chrome, IE10 and Firefox Windows 7/8 browsers Safari iOS Chrome iOS Opera Coast iOS Android Stock Browser Chrome for Android Firefox for Android Opera for Android Dolphin for Android

  • This would only work if toString had not been overridden, no?
  • @jAndy Is this foolproof? I thought toString doesn't work in all modern browsers or something.
  • @joekit if toString is overridden you should be able to do'[native code]'); Also it would probably be a better idea to use RegExp. Try calling the function against itself, and it would come across as native because it appears in the string.
  • When bind is used to bind a method to certain context, the resulting method is though not native but your check would say its native. window.alert = function () {}; window.alert = window.alert.bind()
  • Note this isn't the perfect solution. Checkout
  • Returns false for Promise even though my browser supports promises natively.
  • Yes I don't know why that is. Promise.prototype.constructor does not equal Object.prototype.constructor. I usually don't test if it's native anyway, and look for the then method which is how native promises resolve library promises any way. If all else fails then I use Promise.resolve(nonNative) to normalize all promises.
  • @trusktr Interesting. In Firefox returns [object RegExp]. I don't know about IE, or Opera.
  • I downvoted because I was able to easily make it return true on a non-native function: let f = function() {}; f.prototype = Object.prototype; isNative(f) // true.
  • @trusktr Wouldn't you only do that if you were making a constructor? If you're the one making it then wouldn't you know what it is? There is really no perfect solutions to this particular problem. I've known about the '[native code]' check for a long time, but there are situations where that doesn't work as well. I didn't include that in my answer because someone else already posted it.
  • can you elaborate why your solution is better and how this works?