"未定义的“;当从Android向JavaScript传递数据时

"Undefined" when passing data to JavaScript from Android

本文关键字:JavaScript 数据 当从 未定义 quot Android      更新时间:2023-09-26

我正在使用这段代码,但值显示为"未定义",有人能指出问题吗?

这是我的java类代码

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(com.frux.web.R.layout.activity_main);
    String value = "Isiah";
    WebView web = (WebView) findViewById(R.id.web1);
    web.getSettings().setJavaScriptEnabled(true);
    web.loadUrl("file:///android_asset/www/index.html");
    web.loadUrl("javascript:setValue('""+ value +"'")");

}

这是我的网页代码

<!DOCTYPE html>
<html>
<head>
</head>
<body>
Whats your Name?
<input id="name" value="" />
<button onclick = "setValue()">Submit</button>
<script type="text/javascript"> 
function setValue(value){
var myValue = value;
document.getElementById("name").value = myValue;
}
</script>
</body>
</html>

我也试试这个

web.loadUrl("javascript:setValue('"+ value +"')");

任何想法都将受到的高度赞赏

我使用了这个HTML代码中的代码,它的显示与我发布的上面的其他代码没有什么不同。

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<script type="text/javascript">  
function setValue(amount1)
{
    myValue = amount1;
    document.getElementById("amount").value = myValue;
  }
function rand ( n )
{
    document.getElementById("orderRefId").value =  ( Math.floor ( Math.random ( ) * n + 1 ) );
}

</script>
</head>
<body onLoad="rand(200000);setValue();">
        <!-- 
            Note: https://www.pesopay.com/b2c2/eng/payment/payForm.jsp for live payment URL
                  https://test.pesopay.com/b2cDemo/eng/payment/payForm.jsp for test payment URL
        -->
        <form method="POST" name="frmPayment" action="https://test.pesopay.com/b2cDemo/eng/payment/payForm.jsp">
        <table>
        <tbody>
        <tr>
            <td>Order Reference No. (your reference number for every transaction that has transpired):</td> 
            <td><input type="text" id="orderRefId" name="orderRef" value="Test-001"/></td>
        </tr>   
        <tr>
            <td>Amount:</td>
            <td><input type="text" name="amount" id="amount" value=""/></td>
        </tr>
        <tr>
            <td>Currency Code - "608" for Philippine Peso, "840" for US Dollar:</td>
            <td><input type="text" name="currCode" value="608"/></td>
        </tr>   
        <tr>
            <td>Language:</td>
            <td><input type="text" name="lang" value="E"/></td>
        </tr>   
        <tr>
            <td>Merchant ID (the merchant identification number that was issued to you - merchant IDs between test account and live account are not the same):</td> 
            <td><input type="text" name="merchantId" value="18056869"/></td>
        </tr>
        <tr>    
            <td>Redirect to a URL upon failed transaction:</td>
            <td><input type="text" name="failUrl" value="http://www.yahoo.com?flag=failed"/></td>
        </tr>   
        <tr>
            <td>Redirect to a URL upon successful transaction:</td>
            <td><input type="text" name="successUrl" value="http://www.google.com?flag=success"/></td>
        </tr>
        <tr>
            <td>Redirect to a URL upon canceled transaction:</td>
            <td><input type="text" name="cancelUrl" value="http://www.altavista.com?flag=cancel"/></td>
        </tr>
        <tr>
            <td>Type of payment (normal sales or authorized i.e. hold payment):</td> 
            <td><input type="text" name="payType" value="N"/></td>
        </tr>
        <tr>    
            <td>Payment Method - Change to "ALL" for all the activated payment methods in the account, Change to "BancNet" for BancNet debit card payments only, Change to "GCASH" for GCash mobile payments only, Change to "CC" for credit card payments only:</td>
            <td><input type="text" name="payMethod" value="ALL"/></td>
        </tr>
        <tr>    
            <td>Remark:</td>
            <td><input type="text" name="remark" value="Asiapay Test"/></td>
        </tr>
        <!--<tr>    
            <td>Redirect:</td>
            <td><input type="text" name="redirect" value="1"/></td>
        </tr>-->
        <tr>
            <td></td>
        </tr>   
        <input type="submit" value="Submit">


        </tbody>
        </table>    

        </form>
</body>
</html>             

将传递的值用双引号括起来:

web.loadUrl("javascript:setValue('""+value+"'")");

我明白了!当您第二次调用loadUrl时,页面尚未加载。解决方案是将您的setValue呼叫附加到window.onload事件:

super.loadUrl("javascript:window.onload = function(){setValue('"haha'");};");

此代码将"haha"正确加载到输入中。

试着用单引号包装你的值,比如

 web.loadUrl("javascript:setValue('"+ value +"')");

我在DOM准备好之前尝试访问它时遇到了这个问题。devmilles.com的解决方案有效,但我花了更多的时间进行实验。

webView.loadUrl("javascript:window.addEventListener('DOMContentLoaded',
    function(){setValue('haha');}, false);");
webView.loadUrl("javascript:window.addEventListener('load',
    function(){setValue('haha');}, false);");

对我有效,但将事件附加到document而不是window没有效果。在支持的情况下,DOMContentLoaded的响应速度略快于load

此外,事实证明WebViewClient一直有一个onPageFinished()事件,其工作方式与load事件类似

webView.setWebViewClient(new WebViewClient() {
    public void onPageFinished(WebView view, String url) {
        view.loadUrl("javascript:setValue('haha');");
    }
});

最后,我可以模仿的行为

webView.setWebChromeClient(new WebChromeClient() {
    public void onProgressChanged(WebView view, int progress) {
        if (progress == 100) {
            view.loadUrl("javascript:setValue('haha');");
        }
    }
});

但我认为没有理由将其用于CCD_ 11方法(它们可能等效,也可能不等效)。

我也面临同样的问题。但问题是通过以下方式解决的。

     String testValue="abcdedfg";
    String value = "''"+testValue+"''";
    web.loadUrl("javascript:setValue('""+value+"'")");