\" in a variable in JavaScript", "text": "<p>I want to put "<code>&lt;/script&gt;</code>" in a variable in JavaScript but it will be consider as end tag like below example:</p>\n\n<pre class="prettyprint"><code>&lt;script&gt; \ncontent.value = aval + '&lt;script&gt; ma_gallery("' + varimgurl + '");&lt;/script&gt;'+ sevom;\n&lt;/script&gt;\n</code></pre>\n\n<p>Does anyone know a solution ?</p>", "answerCount": 2, "upvoteCount": 718, "dateCreated": "2014-07-22 16:03:57", "dateModified": "2022-10-05 00:21:05", "author": { "type": "Person", "name": "M.Arjmandi" }, "acceptedAnswer": { "@type": "Answer", "text": "<p>Use <code>'&lt;\\/script&gt;'</code>.</p>\n\n<p>In a string literal, an escaped slash is equivalent to a slash so it makes no difference to the JavaScript parser but it breaks up the end tag syntax so it isn't parsed as an end tag by the HTML parser.</p>\n\n<p>(An alternative is to split it up into two strings and concatenate them together, but that is more typing, harder to read, and (albeit not significantly) less efficient).</p>", "upvoteCount": 122, "url": "https://exchangetuts.com/put-script-in-a-variable-in-javascript-1641121624940678#answer-1657674914752260", "dateCreated": "2022-10-03 11:21:05", "dateModified": "2022-10-05 00:21:05", "author": { "type": "Person", "name": "Quentin" } }, "suggestedAnswer": [ { "@type": "Answer", "text": "<p>Split <code>"&lt;/script&gt;"</code> into two strings:</p>\n\n<pre class="prettyprint"><code>content.value = aval + '&lt;script&gt; ma_gallery("' + varimgurl + '");&lt;/scr' + 'ipt&gt;' + sevom;\n</code></pre>", "upvoteCount": 22, "url": "https://exchangetuts.com/put-script-in-a-variable-in-javascript-1641121624940678#answer-1657674914755470", "dateCreated": "2022-10-03 11:21:05", "dateModified": "2022-10-05 00:21:05", "author": { "type": "Person", "name": "Danilo Valente" } } ] } }
Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

put "</script>" in a variable in JavaScript

I want to put "</script>" in a variable in JavaScript but it will be consider as end tag like below example:

<script>     
content.value = aval + '<script> ma_gallery("' + varimgurl + '");</script>'+ sevom;
</script>

Does anyone know a solution ?

like image 718
M.Arjmandi Avatar asked Jul 22 '14 16:07

M.Arjmandi


People also ask

How do you add something to a variable in JavaScript?

Add numbers in JavaScript by placing a plus sign between them. You can also use the following syntax to perform addition: var x+=y; The "+=" operator tells JavaScript to add the variable on the right side of the operator to the variable on the left.

How set JavaScript variable value in HTML?

To add the content of the javascript variable to the html use innerHTML() or create any html tag, add the content of that variable to that created tag and append that tag to the body or any other existing tags in the html.

How do you declare a variable in script tag?

4 Ways to Declare a JavaScript Variable:Using var. Using let. Using const. Using nothing.

How do I pass a variable from one script to another in JavaScript?

In JavaScript, variables can be accessed from another file using the <script> tags or the import or export statement. The script tag is mainly used when we want to access variable of a JavaScript file in an HTML file. This works well for client-side scripting as well as for server-side scripting.


2 Answers

Use '<\/script>'.

In a string literal, an escaped slash is equivalent to a slash so it makes no difference to the JavaScript parser but it breaks up the end tag syntax so it isn't parsed as an end tag by the HTML parser.

(An alternative is to split it up into two strings and concatenate them together, but that is more typing, harder to read, and (albeit not significantly) less efficient).

like image 122
Quentin Avatar answered Oct 05 '22 00:10

Quentin


Split "</script>" into two strings:

content.value = aval + '<script> ma_gallery("' + varimgurl + '");</scr' + 'ipt>' + sevom;
like image 22
Danilo Valente Avatar answered Oct 05 '22 00:10

Danilo Valente