1<html> 2<!-- Copyright (C) 2019 The Android Open Source Project 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15--> 16<head> 17 <title>Load html over XMLHttpRequest</title> 18</head> 19<body> 20 <!-- Ensure HTML elements exist before executing JavaScript --> 21 <h1 id="title">Loaded HTML should appear below on success</h1> 22 <hr> 23 <div id="assets_html"></div> 24 <div id="res_html"></div> 25 26 <!-- Load html file from android_assets --> 27 <script type="text/javascript"> 28 var xhr = new XMLHttpRequest(); 29 xhr.onreadystatechange = function() { 30 if (this.readyState === 4 && this.status === 200) { 31 document.getElementById("assets_html").innerHTML = this.responseText; 32 } else if (this.status !== 200) { 33 document.getElementById("assets_html").innerHTML = 34 "Loading file from assets failed (status=" + this.status + ")"; 35 } 36 }; 37 xhr.onerror = function() { 38 document.getElementById("assets_html").innerHTML = "Loading file from assets failed"; 39 }; 40 xhr.open("GET", 'https://example.com/androidx_webkit/example/assets/www/some_text.html', true); 41 xhr.send(); 42 </script> 43 44 <!-- Load html file from android_resources --> 45 <script type="text/javascript"> 46 var xhr = new XMLHttpRequest(); 47 xhr.onreadystatechange = function() { 48 if (this.readyState === 4 && this.status === 200) { 49 document.getElementById("res_html").innerHTML = this.responseText; 50 } else if (this.status !== 200) { 51 document.getElementById("res_html").innerHTML = 52 "Loading file from resources failed (status=" + this.status + ")"; 53 } 54 }; 55 xhr.onerror = function() { 56 document.getElementById("res_html").innerHTML = "Loading file from resources failed"; 57 }; 58 xhr.open("GET", 'https://example.com/androidx_webkit/example/res/raw/some_text.html', true); 59 xhr.send(); 60 </script> 61</body> 62</html> 63