Android - Webview (kitkat and below): Error In Jav

2019-08-26 23:16发布

问题:

Trying to Call WebView , While Calling Local Web Page It will Gives Error

Uncaught TypeError: Object ["Some Object"] has no method : 'includes', source: file:///storage/sdcard0/MyDemo/js/fileName.js

It will gives Error To Kitkat and Below. It works Properly above Kitkat.

It was working Fine With gradle Settings below

compileSdkVersion 25
buildToolsVersion "25.0.2"

and Dependancy

compile 'com.android.support:design:25.3.1'
compile 'com.android.support:support-v4:25.3.1

Currently Using Gradle settings.

compileSdkVersion 26
buildToolsVersion "26.0.2"
defaultConfig {
    applicationId ""
    minSdkVersion 16
    targetSdkVersion 26
    versionCode 1
    versionName "1.0"
    multiDexEnabled true    
}

compile 'com.android.support:design:26.1.0'
compile 'com.android.support:support-v13:26.1.0'
compile 'com.android.support:appcompat-v7:26.1.0'

回答1:

This is happening because you are using includes method on string in your js file, which is supported in latest JavaScript Version ECMAScript 6 and android kitkat webview doesn't support this version.

You need to make use of indexOf instead includes

var str = "Hello world, welcome to the universe.";

var n = str.includes("world");
# Replace above line of code and use indexOf. 

var n = str.indexOf("world") > -1;