I have a web-service In that I have added bar-code reader for android
So with the Help of JavaScript I am calling my bar-code reader from web-view
So for that I followed this
and designed on server side... I have Given this
at JavaScript
function mybarcd() {
MyApp.mybarcdt();
}
function actfromAnd(msg){
document.getElementById("brcd").value = msg;
}
at HTML/PHP
<div class="data">
<input id="brcd" type="text" value=""/>
<button type="button" onClick="mybarcd()">SCAN</button>
</div>
On Android side In webview
webView.addJavascriptInterface(new WebAppInterface(this), "MyApp");
and new js interface
@JavascriptInterface
public void mybarcdt() {
IntentIntegrator intentIntegrator = new IntentIntegrator(Main_A.this);
intentIntegrator.setBeepEnabled(true);
intentIntegrator.setOrientationLocked(true);
intentIntegrator.setPrompt("Scan Barcode");
intentIntegrator.initiateScan();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (result != null) {
if (result.getContents() == null) {
Log.d("ScanActivity", "Cancelled scan");
Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();
} else {
Log.d("ScanActivity", "Scanned");
String bcval = result.getContents();
if (android.os.Build.VERSION.SDK_INT < 19) {
webView.loadUrl("javascript:actfromAnd(\""+bcval+"\")");
}else{
webView.evaluateJavascript("javascript:actfromAnd(\""+bcval+"\")", null);
}
System.out.println("javascript:actfromAnd(\""+bcval+"\")");
}
} else
super.onActivityResult(requestCode, resultCode, data);
}
My Problem is that Its working fine in a single Html/PHP file with Js on same page or separate page I have tested its scanning and Updating the value in input box...
But its not working since I have using multiple pages or frame in one webview... its missing JS value... How ever form server its opening scanner at on-click... but after scanning the value is not passing to the input box with JS I am getting this error.....
I/chromium: [INFO:CONSOLE(1)] "Uncaught ReferenceError: actfromAnd is not defined", source: (1)
Update
1)I hava Tried this in Static Page with JS in side that PHP/HTML page
2)I also tried with same in a static Page with JS seperate page
On the above two conditions its worked fine
But In my web-service I have Given Same JS file which is running successfully in Static page I have a single JS file for My Webservice and Static page its working fine in static but not working in MY webservice live.. How ever JS is loading Because on click its wokring from that JS and its opening Camera But responce after scanning its not going to web input
I understand that I am getting Error Because...
In my Live Page I have a MainMenu Inside that menu when I select a application its loading in
iframe
So my Android Activity responce after scanning Is pinging to that Mainmenu page But for menu there is no Js function namedactfromAnd
So I am getting Error...
Here I can't give URL of that particular page(iframe
) Because of depending on the menus it will change I can Give Only Login or MainMenu link directly.but not for a particular page inside the menu
Can Any one suggest me on this kind...
Add this script in Web-service at your Parent Menu which has your iframe
or iframes
<script>
function actfromAnd(msg){
window.frames['yourExactframe'].document.getElementById("brcd").value = msg;
}
</script>
If you are using same in more than one frame then declare your frame name globally
I tried your Code Working Fine... In my example Frame Hope It works fine for you
Nice question....
You should execute the javascript when the page is loaded
mWebView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
webview.loadUrl("javascript:myFunction()");
}
});
The code will be executed and will find your javascript function. The way you are doing now does not wait.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With