Possiamo prima caricare il file di testo esterno in un IFRAME nascosto, facente parte della pagina, poi - posto che entrambi i file risiedano sullo stesso pc/rete/server - accedere all' IFRAME per recuperarne il contenuto.
Prova questo così com'è: i metodi per accedere al contenuto caricato di un IFRAME variano a seconda del tipo di navigatore, e anche in base al !DOCTYPE
.contentWindow in quirks-mode è (abbastanza?) cross-browser.
codice:
<html>
<title>leopard's Includable</title>
<script>
function extract(){
var theLoader = document.getElementById('loader');
var theShower = document.getElementById('shower');
theShower.innerHTML = theLoader.contentWindow.document.body.innerHTML;
alert(theShower.innerHTML);
}
</script>
<style>
#shower pre {
color: white;
font-family: Verdana, Helvetica;
}
</style>
<body bgcolor="seagreen" onload="extract();">
<h1>Text File JS-Embed — Load external .txt file to display as formatted content</h1><hr>
<iframe id="loader" src="leopard.txt" style="display: none"></iframe>
<div id="shower"></div>
</body>
</html>
NON si DOVREBBE presentare il problema per cui, se il compilatore del leopard.txt scrivesse dei tag, questi passino ad essere regolarmente processati nel DIV ID="Shower" potendo "squassare" tutta la pagina; ci avvarrebbe allora della funzione replace()