Is there an alternative to innerHTML? There is a code coming from the server: PHP:

$html = ' Тут див с фотографией <script src="../assets/js/app.js"></script> <script src="../assets/js/plugins/magnific-popup/magnific-popup.min.js"></script> <script> jQuery(function () { App.initHelpers(\'magnific-popup\'); }); </script> '; 

We get it and need to display it.

 document.getElementById('content').innerHTML = data.html; 

But there is a problem. Если в innerHTML есть тег script – он не будет выполнен. If it is output via append, then everything works, but the text is added to the already existing text, and a replacement is needed, as innerHTML does

  • but you can not clear the content and then append to do? - teran

3 answers 3

Since you have jQuery, use it. It executes scripts when pasting through $smth.html(someHtml) .

    How about creating a new child object?

     function createLabel(val) { var a = document.createElement("a"); a.innerHTML = val; document.getElementById(id_of_element).appendChild(a); } 

    Here also via appendChild , but there is no other way out (except document.write , but it erases everything else. You can preset spaces ("text with a space in front") or use /n

       document.getElementById('content').innerHTML = ""; // потом append