Beispiel:Fetch.html

Aus SELFHTML-Wiki
Wechseln zu: Navigation, Suche

<!DOCTYPE html> <html lang="de"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Beispiel Fetch</title> <style> pre { tab-size: 1em; } </style> <script> document.addEventListener("DOMContentLoaded", () => { const output = document.querySelector("output"); const filename = location.href; // Für diesen Test wird die aktuelle Seite geladen

fetch(filename) .then(response => { if (!response.ok) { console.error(`HTTP error: ${response.status}`);

     				throw new Error(`HTTP error: ${response.status}`);
   				}

return response.text(); }) .then(text => {

output.innerHTML = "
" + text.replace(/</g,"<") + "
";

}) .catch(error => console.error(`Error fetching ${filename}: ${error}`)); }); </script> </head> <body>

Beispiel Fetch

Einlesen des Quelltextes dieser Seite

<output></output> </body> </html>