Text in javascript with 'script / script'

1

I am adding a script in html and within a string of the style. I do this because we show a piece of code on our platform that we want the user to be able to copy by clicking on a copy button:

<script>
    function copyCode(){
            $("body").append($temp);
            $temp.val("<div id=\"1"></div>"+
                    "<script>(function(a,b,c) {
                      ...
                     }("a", "b", "c"));
                     </"+"script>").select();
            document.execCommand("copy");
            $temp.remove();
    }
</script>

The problem is that closing the script of the variable I created gives me an error (as if I assumed that the closing of the script inside the variable is the end of the script ...).

Do you know any way to solve it?

Currently the solution I have is this:

var text = '<script> ... </'+'script>';

Edit: I have added the code more fully as I have explained the problem better.

    
asked by Felix 15.06.2018 в 10:04
source

1 answer

4

If you want to display html / js code as text on a page you can escape the symbols < and > , using &lt; (from l ess t have, less than) and &gt; (of g reater t have , greater than).

Here is a simple example supported by the <pre> tag:

$('#texto').html('<pre>
&lt;script src="/jquery.min.js"&gt;&lt;/script&gt;
</pre>');
pre {
background-color: lightgray;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="texto">

There is a long list of special characters that can be displayed using HTML Entities

    
answered by 15.06.2018 в 10:49