\" 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><script></code> as a substring in a javascript string!</em> Here is an example:</p> <pre class="prettyprint"><code><html> <head> <script> alert("<script></script>"); </script> </head> </html> </code></pre> <p>It supposed to print out <strong><code><script></script></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><script></code> followed by <code></script></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" } } }
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With