使用Android Webview检查此值将值保存到Android浏览器上的本地存储中

时间:2014-10-09 08:32:59

标签: android webview local-storage

我将使用Android Webview检查此值在将值保存到Android浏览器上的本地存储后。

<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<title>HTML5</title>
<script>
    window.onload = function(){
        loadStorage();
        document.querySelector("form").onsubmit = saveStorage;
    };

    function saveStorage(){
        var saveId = document.getElementById("saveId").checked;
        var userId = document.getElementById("userId").value;
        if(saveId){
            window.localStorage.setItem("userId", userId);
            window.localStorage.setItem("userIdSaved", true);
        }else{
            window.localStorage.removeItem("userId");
            window.localStorage.setItem("userIdSaved", false);
        }
    }
    function loadStorage(){
        var userId = window.localStorage.getItem("userId");
        document.getElementById("userId").value = userId;

        if(userId!=null){
            document.getElementById("saveId").checked = true;
        }
    }
</script>
</head>
<body>
    <h1>Login(Web Storage)</h1>
    <form action="login.php" method="post">
        <fieldset>
            id: <input type="text" name="id" id="userId" autocomplete="off">
            <input type="checkbox" id="saveId">Save ID<br>
            pass: <input type="password"><br>
            <input type="submit" value="login">
        </fieldset>
    </form>
</body>
</html>

但是,如果您运行webview而不输出存储在Android浏览器中的值。

通过执行Webview检查值,但不保存

中的本地存储
<html>
<head>
<meta charset="UTF-8">
<title>HTML5</title>
</head>
<?
$Id = $_POST['id'];
?>
<script>
    window.onload = function(){
        var userId = window.localStorage.getItem("userId");
        alert(userId);
         init();
    };
    function init(){

           var list = document.getElementById( "list");
           list.innerHTML = "";

            for( var i = 0; i < localStorage.length ; i++){
                 var key = localStorage.key(i);
                list.options[list.options.length] = new Option(localStorage[key],key);
           }
     }
</script>
<body>
    <h1>Result</h1>
    <p>Welcome to <?=$Id?> </p>

    <a href="index.html">home</a><br/>
    <select id = "list" size= "10"></ select>


      <fieldset >
           key : <input type = "text" id= "key"/>
           value : <input type = "text" id= "value"/>
      </fieldset >
</body>
</html>

要显示存储在Webview中的值,我该怎么办?

1 个答案:

答案 0 :(得分:0)

我不确定您是否可以在WebView中使用web localStorage。但是,您可以在应用程序首选项中存储值。在自定义Javascript接口中实现相应的方法。见https://stackoverflow.com/a/10389678/527759