"未定义的“;当从Android向JavaScript传递数据时
"Undefined" when passing data to JavaScript from Android
我正在使用这段代码,但值显示为"未定义",有人能指出问题吗?
这是我的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+"'")");
相关文章:
- JavaScript数据结构
- 父级对子 JavaScript 数据的访问
- 更改动态 JavaScript 数据
- Javascript 数据可用事件
- 将javascript数据放入表单中
- 将json结构转换为可用的javascript数据集
- 从HTML表单中获取计算得到的JavaScript数据,并将其作为新元素添加到相同的表单操作中以使用PHP保存
- 数值的 JavaScript 数据类型
- 使用 ajax 将 JavaScript 数据传输到 PHP
- Javascript数据将元素属性绑定到URL中的锚href
- 整页回发和javascript数据
- 如何使用javascript数据对象?如何通过变量访问条目
- 如何将从数据库中提取的数字传递到javascript数据部分
- Spring MVC与Google图表,创建javascript数据表的最佳方式是什么
- JavaScript 数据中的 XSS
- 无法将我的 JavaScript 数据填充为 HTML
- 如何将相同的 JavaScript 数据传递给 HTML 两种不同的方式
- JavaScript 数据结构,JS中的ArrayList(Java)
- 将 casper javascript 数据插入 CSV 文件
- 用于高效插入和搜索的 Javascript 数据结构