Discussion Forums  >  WebViews and HTML for Mobile

Replies: 2    Views: 149

AlanMac
Aspiring developer
Profile
Posts: 2612
Reg: Mar 05, 2012
Esher, UK
37,120
09/18/12 08:22 AM (13 years ago)

Forms/JavaScript in Android BT2.0

I've been trying to develop a form-to-email solution as a Custom HTML / Text document. I have pulled something together that uses a local form (which breaks the on-line editor but that is another story). The form uses a bit of JavaScript (embedded in the html, not external references) to validate fields and a formmail script on a server. It works really well on an iPhone, but not on Android and I cannot figure out why. I then thought I might do some browser detection in the html page with JavaScript. I was thinking that if I can detect the device is Android, I could then jump to an external, hosted html form (works but slower). That detection works OK on the iPhone, but not on Android. I don't want to compromise the iPhone experience by using an externally hosted form, but I was wondering if it might be the only way for Android. I beginning to think it there are JavaScript differences between Android and IOS that I should be aware of (and I am not) or an Eclipse (Mac, Juno Release) issue. Any suggestions would be gratefully received. Thanks, Alan
 
Ledbelly2142
Aspiring developer
Profile
Posts: 80
Reg: Aug 04, 2012
Reno
6,750
like
09/18/12 08:41 AM (13 years ago)
Im pretty clueless, but I googled and found this: http://developer.android.com/reference/android/webkit/WebView.html#addJavascriptInterface%28java.lang.Object,%20java.lang.String%29 Looks like javascript may need to be "enabled" in the native browser in some way. I hope this helps. Public Methods public void addJavascriptInterface (Object object, String name) Since: API Level 1 Injects the supplied Java object into this WebView. The object is injected into the JavaScript context of the main frame, using the supplied name. This allows the Java object's public methods to be accessed from JavaScript. Note that that injected objects will not appear in JavaScript until the page is next (re)loaded. For example: webView.addJavascriptInterface(new Object(), "injectedObject"); webView.loadData("", "text/html", null); webView.loadUrl("javascript:alert(injectedObject.toString())"); IMPORTANT: This method can be used to allow JavaScript to control the host application. This is a powerful feature, but also presents a security risk, particularly as JavaScript could use reflection to access an injected object's public fields. Use of this method in a WebView containing untrusted content could allow an attacker to manipulate the host application in unintended ways, executing Java code with the permissions of the host application. Use extreme care when using this method in a WebView which could contain untrusted content. JavaScript interacts with Java object on a private, background thread of this WebView. Care is therefore required to maintain thread safety. Parameters object the Java object to inject into this WebView's JavaScript context. Null values are ignored. name the name used to expose the object in JavaScript
 
AlanMac
Aspiring developer
Profile
Posts: 2612
Reg: Mar 05, 2012
Esher, UK
37,120
like
09/19/12 12:55 PM (13 years ago)
Thanks for looking for me @Ledbelly2142, I believe the android browser will run javascript fine, but it seems once the app has been through eclipse, the javascript in the local html page doesn't work. I've decided to go for a form with no javascript and rely on server side validation for now, but I'd like to know if anyone else has experienced this and what can be done.
 

Login + Screen Name Required to Post

pointerLogin to participate so you can start earning points. Once you're logged in (and have a screen name entered in your profile), you can subscribe to topics, follow users, and start learning how to make apps like the pros.