WebView.LoadData(String, String, String) Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Loads the given data into this WebView using a 'data' scheme URL.
[Android.Runtime.Register("loadData", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V", "GetLoadData_Ljava_lang_String_Ljava_lang_String_Ljava_lang_String_Handler")]
public virtual void LoadData (string data, string? mimeType, string? encoding);
[<Android.Runtime.Register("loadData", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V", "GetLoadData_Ljava_lang_String_Ljava_lang_String_Ljava_lang_String_Handler")>]
abstract member LoadData : string * string * string -> unit
override this.LoadData : string * string * string -> unit
Parameters
- data
- String
a String of data in the given encoding
- mimeType
- String
the MIME type of the data, e.g. 'text/html'.
- encoding
- String
the encoding of the data
- Attributes
Remarks
Loads the given data into this WebView using a 'data' scheme URL.
Note that JavaScript's same origin policy means that script running in a page loaded using this method will be unable to access content loaded using any scheme other than 'data', including 'http(s)'. To avoid this restriction, use #loadDataWithBaseURL(String,String,String,String,String) loadDataWithBaseURL()
with an appropriate base URL.
The encoding
parameter specifies whether the data is base64 or URL encoded. If the data is base64 encoded, the value of the encoding parameter must be "base64"
. HTML can be encoded with android.util.Base64#encodeToString(byte[],int)
like so:
String unencodedHtml =
"<html><body>'%28' is the code for '('</body></html>";
String encodedHtml = Base64.encodeToString(unencodedHtml.getBytes(), Base64.NO_PADDING);
webView.loadData(encodedHtml, "text/html", "base64");
<p class="note"> For all other values of encoding
(including null
) it is assumed that the data uses ASCII encoding for octets inside the range of safe URL characters and use the standard %xx hex encoding of URLs for octets outside that range. See RFC 3986 for more information. Applications targeting android.os.Build.VERSION_CODES#Q
or later must either use base64 or encode any #
characters in the content as %23
, otherwise they will be treated as the end of the content and the remaining text used as a document fragment identifier.
The mimeType
parameter specifies the format of the data. If WebView can't handle the specified MIME type, it will download the data. If null
, defaults to 'text/html'.
The 'data' scheme URL formed by this method uses the default US-ASCII charset. If you need to set a different charset, you should form a 'data' scheme URL which explicitly specifies a charset parameter in the mediatype portion of the URL and call #loadUrl(String)
instead. Note that the charset obtained from the mediatype portion of a data URL always overrides that specified in the HTML or XML document itself.
Content loaded using this method will have a window.origin
value of "null"
. This must not be considered to be a trusted origin by the application or by any JavaScript code running inside the WebView (for example, event sources in DOM event handlers or web messages), because malicious content can also create frames with a null origin. If you need to identify the main frame's origin in a trustworthy way, you should use #loadDataWithBaseURL(String,String,String,String,String) loadDataWithBaseURL()
with a valid HTTP or HTTPS base URL to set the origin.
Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.