从Android传递数据到JavaScript时“未定义”

时间:2012-08-02 09:26:59

标签: javascript android android-webview

我正在使用此代码但是值“未定义”可以让任何人指出我的问题吗?

这是我的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>             

4 个答案:

答案 0 :(得分:1)

将传递的值换成双引号:

web.loadUrl("javascript:setValue(\""+value+"\")");
我得到了这个!当您第二次呼叫loadUrl时,页面尚未加载。解决方案是将setValue来电添加到window.onload事件:

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

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

答案 1 :(得分:0)

尝试将您的值包装在单引号中

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

答案 2 :(得分:0)

在尝试访问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');");
        }
    }
});

但我认为没有理由在WebViewClient方法上使用它(它们可能相同或不同)。

答案 3 :(得分:0)

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

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