0

I am making a code editor similar to Codepen, however when I try to make my Javascript textarea to say </script>, it will think I want to end the script of the actual program.

<script>
    function compile() {
      var html = document.getElementById("html");
      var css = document.getElementById("css");
      var js = document.getElementById("js");
      var code = document.getElementById("code").contentWindow.document;

          submit = () => {
              code.open();
              code.writeln(
              html.value +
                  "<style>" +
                  css.value +
                  "</style>" +
                  "<script>" +
                  js.value +
                  "</script>"
              );
              code.close();
          };
      }
      compile();
  </script>

I understand this could be easily fixed by instead making this a separate .js file, but I want to know how to put this script in an HTML file without it thinking "" is to end the actual html files script.

Thank you in advance

VLAZ
  • 26,331
  • 9
  • 49
  • 67
  • Put the ending tag like this: `<\/script>`. See also https://stackoverflow.com/questions/236073/why-split-the-script-tag-when-writing-it-with-document-write – Teemu Feb 18 '22 at 06:33
  • Try like this doing it in a string template literal form: const submit = () => { code.open(); code.writeln(` ${html.value} `); code.close(); }; } – Zaf Feb 18 '22 at 06:51
  • or `const doc = document.getElementById("code").contentWindow.document; const src = doc.createElement('script'); scr.textContent = js.value; doc.querySelector('head').appendChild(scr);` – mplungjan Feb 18 '22 at 07:08

0 Answers0