Using JSInterface to load a web form?

lagore

Active Member
Licensed User
Longtime User
Hi,
Is it possible to use JSInterface to load fields in a web form loaded into a webview. I was hoping to use it to fill 'username & password' and then activate the button click on the web form on a site that I am having trouble doing directly using httpUtils due to a redirection problem I have not sorted yet.
Edward
 

warwound

Expert
Licensed User
Longtime User
Hi.

If your form is properly constructed it should be possible.

Let's say this is your form:

B4X:
<form action="" id="myForm">
<input type="text" value="" name="username" />
<input type="password" value="" name="password" />
<input type="submit" value="Go" />
</form>

You can execute javascript to set the inputs values and also call the form's submit() method:

B4X:
<script type="text/javascript">
var myForm=document.forms.myForm;
myForm.username.value='Edward';
myForm.password.value='1234567890';
myForm.submit();
</script>

To put that all together, you can execute the javascript either using JSInterface and it's execJS() method:

B4X:
Dim myInterface As JSInterface
Dim javascript As String
javascript="var myForm=document.forms.myForm;myForm.username.value='Edward';myForm.password.value='1234567890';myForm.submit();"
myInterface.execJS(myWebView, javascript)

OR you can execute the javascript using the WebView's LoadUrl() method:

B4X:
Dim javascript As String
javascript="javascript:var myForm=document.forms.myForm;myForm.username.value='Edward';myForm.password.value='1234567890';myForm.submit();"
myWebView.LoadUrl(javascript)

Martin.
 
Upvote 0

lagore

Active Member
Licensed User
Longtime User
Hi Martin,
Your code worked perfectly, now I can load the login web page run your bit of JSInterface magic and login to the site which gets over the problem I was having with site redirection.
Thanks
Edward :sign0098:
 
Upvote 0
Top