Issue
I have a simple login webpage which loads up in a WebView in a ContentPage in Xamarin. I have added the JavaScript renderer as shown here (https://www.xamarinhelp.com/xamarin-forms-webview-executing-javascript/) and it works fine. I am able to retrieve the values of the textbox in this way:
var email = await EvaluateJavascript("document.getElementById('Email').value;");
But is there a way in which I can set the default value of the textbox when the WebView loads. Making changes to the html of the WebView is not an option here.
I tried doing this but it doesn't work :
await EvaluateJavascript("document.getElementById('Email').value = '[email protected]';");
Solution
is there a way in which I can set the default value of the textbox when the WebView loads. Making changes to the html of the WebView is not an option here.
This answer is No, It cannot be achieved. If we want to set the value for textBox from html. This webview This webview must be finished loading.
So when you used await EvaluateJavascript("document.getElementById('Email').value = '[email protected]';");
it not worked.
If we use await EvaluateJavascript("document.getElementById('Email').value = '[email protected]';");
when webview is finished loading.it will worked.
So we can create webview's custom renderer. Override the OnPageFinished
method in MyWebviewClient
, if page finshed loading, we can send an messageCenter.
[assembly: ExportRenderer(typeof(WebView), typeof(MyWebview))]
namespace XFormsWebview.Droid
{
class MyWebview : WebViewRenderer
{
public MyWebview(Context context) : base(context)
{
}
protected override void OnElementChanged(ElementChangedEventArgs<WebView> e)
{
base.OnElementChanged(e);
Control.Settings.JavaScriptEnabled = true;
Control.SetWebViewClient(new MyWebviewClient());
}
}
internal class MyWebviewClient : Android.Webkit.WebViewClient
{
public override void OnPageFinished(Android.Webkit.WebView view, string url)
{
base.OnPageFinished(view, url);
MessagingCenter.Send<App, string>(App.Current as App, "OpenPage", "finish");
}
}
}
In the PCL, if we get the message. we can set it value by mywebview.EvaluateJavaScriptAsync("document.getElementById('txt').value= 'my url that I want to copy';");
.
namespace XFormsWebview
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
mywebview.Source = LoadHTMLFileFromResource();
MessagingCenter.Subscribe<App, string>(App.Current, "OpenPage", (snd, arg) =>
{
Device.BeginInvokeOnMainThread(() => {
mywebview.EvaluateJavaScriptAsync("document.getElementById('Email').value= '[email protected]';");
});
});
}
HtmlWebViewSource LoadHTMLFileFromResource()
{
var source = new HtmlWebViewSource();
// Load the HTML file embedded as a resource in the .NET Standard library
var assembly = typeof(MainPage).GetTypeInfo().Assembly;
var stream = assembly.GetManifestResourceStream("XFormsWebview.indexd.html");
using (var reader = new StreamReader(stream))
{
source.Html = reader.ReadToEnd();
}
return source;
}
}
}
Here is my html page.
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>www.test.net</title>
</head>
<body>
<script language="javascript">
function c1(myvalue)
{
var t=document.getElementById("Email");
t.value="ttt";
}
</script>
<input type="text" id="Email" value="init content" size="30"/>
<input type="button" value="change" name="btn" onclick="c1();" />
</body>
</html>
Here is running screenshot.
Answered By - Leon Lu - MSFT
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.