Как вывести html-код текстом

При таком выводе в фрейме рендерится полностью страница, как сделать что бы был только текст.

    function displayFileContent(){
        fetch(filePath)
            .then(response => response.text())
            .then(data => {
                var iframe = document.getElementById('contentContainer');
                iframe.srcdoc = data;
            });
    }


Ответы (1 шт):

Автор решения: AfteRDay
    function escapeHTML(str) {
    var map = {
        '&': '&',
        '<': '&lt;',
        '>': '&gt;',
        '"': '&quot;',
        "'": '&#039;'
    };
    return str.replace(/[&<>"']/g, function(m) { return map[m]; });
}

function displayFileContent(){
    fetch(filePath)
        .then(response => response.text())
        .then(data => {
            var iframe = document.getElementById('contentContainer');
            iframe.srcdoc = escapeHTML(data);
        });
}
→ Ссылка