Search code examples
javascriptandroidlegacy

Old Android versions browsers do not have some JS functions?


Having tested an app with a webview containing a html file on a Oreo device, everything works fully. However, when using a Kitkat device (emulator), I am getting the error Possible Unhandled Promise Rejection: "Object function Object() { [native code] } has no method 'entries'".

This was ostensibly due to me having used Object.entries() in the html. Is there any way to work around this? The Browser version on the Kitkat emulator is 4.4.2-4729339 and the emulator is the default emulator on Android Studio.


Solution

  • To make new functions work in older browsers you can use so called Polyfills those are mostly to find on github or on Mozilla Developer Network.

    What you are looking for is a Object.entries() polyfill:

    if (!Object.entries)
      Object.entries = function( obj ){
        var ownProps = Object.keys( obj ),
            i = ownProps.length,
            resArray = new Array(i); // preallocate the Array
        while (i--)
          resArray[i] = [ownProps[i], obj[ownProps[i]]];
    
        return resArray;
      };
    

    (Code taken from: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries#Polyfill)

    In general I recommend you to use caniuse.com where you can check what browser supports which function.