小编Ali*_*Ali的帖子

使用webview时,HTML localStorage在Android上为null

我试图从我的本地Web APP使用localstorage,这是WebView初始化部分:

static class MyWebView extends WebView
        {
            public MyWebView(Context context)
            {
            super(context);

            this.getSettings().setJavaScriptEnabled(true);

            //enable support for DOM Storage and Database
            this.getSettings().setDatabaseEnabled(true);
            this.getSettings().setDomStorageEnabled(true);


            String databasePath = context.getDir("database", Context.MODE_PRIVATE).getPath();

            this.getSettings().setDatabasePath(databasePath);


            this.setVerticalScrollbarOverlay(true);
        } }
Run Code Online (Sandbox Code Playgroud)

这是JavaScript上的测试应用程序:

function testStorage() 
    {
        var storage = window.localStorage;
        if (storage) {
            try {
                storage.setItem("name", "Hello World!"); //saves to the database, “key”, “value”
            } catch (e) {

                    alert(e.message); //data wasn’t successfully saved due to quota exceed so throw an error
            }
            document.write(storage.getItem("name")); //Hello World!
            storage.removeItem('name'); 
        }
        else {
            alert('Your …
Run Code Online (Sandbox Code Playgroud)

javascript android webview local-storage

7
推荐指数
1
解决办法
2万
查看次数

标签 统计

android ×1

javascript ×1

local-storage ×1

webview ×1