\" in a javascript string?", "text": "<p>I've been trying some tricks in javascript and came to a ridiculous problem: <em>I can't use <code>&lt;script&gt;</code> as a substring in a javascript string!</em> Here is an example:</p> <pre class="prettyprint"><code>&lt;html&gt; &lt;head&gt; &lt;script&gt; alert("&lt;script&gt;&lt;/script&gt;"); &lt;/script&gt; &lt;/head&gt; &lt;/html&gt; </code></pre> <p>It supposed to print out <strong><code>&lt;script&gt;&lt;/script&gt;</code></strong>, but instead, I get this:</p> <pre class="prettyprint"><code>"); </code></pre> <p>Printed out on the page, as HTML.</p> <p><strong><em>Question:</em></strong> How can I use <code>&lt;script&gt;</code> followed by <code>&lt;/script&gt;</code> substrings in Javascript, and why is it acting that way?</p> <p>Here is JSFiddle of it.</p>", "answerCount": 0, "upvoteCount": 824, "dateCreated": "2015-02-01 04:13:44", "dateModified": "1970-01-01 00:00:00", "author": { "type": "Person", "name": "Victor2748" } } }
Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to put "</script>" in a javascript string?

Tags:

People also ask

Where do I put scripts in JavaScript?

JavaScript in <head> or <body> You can place any number of scripts in an HTML document. Scripts can be placed in the <body> , or in the <head> section of an HTML page, or in both.

How do I run a script from a string?

Create a New Function with the Function Constructor const code = "alert('Hello World'); let x = 100"; const F = new Function(code); F(); to create the code string and store it into the code variable. Then we pass code into the Function constructor to create a function from it. And then we call F to run the function.

How do I reference an HTML file using JavaScript?

To include an external JavaScript file, we can use the script tag with the attribute src . You've already used the src attribute when using images. The value for the src attribute should be the path to your JavaScript file. This script tag should be included between the <head> tags in your HTML document.

How do you input a string in JavaScript?

In JavaScript, there are three ways to write a string — they can be written inside single quotes ( ' ' ), double quotes ( " " ), or backticks ( ` ` ).


I've been trying some tricks in javascript and came to a ridiculous problem: I can't use <script> as a substring in a javascript string! Here is an example:

<html>     <head>         <script>             alert("<script></script>");         </script>     </head> </html> 

It supposed to print out <script></script>, but instead, I get this:

"); 

Printed out on the page, as HTML.

Question: How can I use <script> followed by </script> substrings in Javascript, and why is it acting that way?

Here is JSFiddle of it.