如何将“”放入javascript字符串中?

时间:2022-12-06 09:44:31

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:

我在javascript中尝试了一些技巧,却遇到了一个可笑的问题:我不能在javascript字符串中使用

<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.

打印在页面上,作为HTML。

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

问题:如何使用子字符串,在字符串中,在Javascript中,它为什么这样做?

Here is JSFiddle of it :)

下面是JSFiddle:

2 个解决方案

#1


16  

What's tripping you up is the </script>. The HTML parser doesn't recognize Javascript strings or nested <script> tags, so it's interpreting that as the closing tag for the initial <script>. That is, this part of the document is parsed as:

将您绊倒的是。HTML解析器不识别Javascript字符串或嵌套的

<script>                (open tag)
    alert("<script>     (text node - contents of the script)
</script>               (close tag)
");                     (text node - plain text)

The second </script> is ignored, as there's no other <script> tag for it to close.

第二个被忽略,因为没有其他的

To work around this, break up </script so that the HTML parser doesn't see it. For instance:

要解决这个问题,可以分解

alert("<script><\/script>");

or:

或者:

alert("<script><" + "/script>");

or just put the code in an external Javascript file. This issue only arises for inline scripts.

或者把代码放在一个外部的Javascript文件中。这个问题只出现在内联脚本中。

#2


1  

it is because of the \ I believe. i have no concrete explanation since I am a newbie to Javascript but this code should work:

这是因为我相信。我没有具体的解释,因为我是Javascript的新手,但是这段代码应该可以工作:

alert("<script><\/script>");

came up with it using Java knowledge.. Haha since the \ is an escape key in many languages.

使用Java知识提出它。哈哈,因为它是许多语言中的一个转义键。

#1


16  

What's tripping you up is the </script>. The HTML parser doesn't recognize Javascript strings or nested <script> tags, so it's interpreting that as the closing tag for the initial <script>. That is, this part of the document is parsed as:

将您绊倒的是。HTML解析器不识别Javascript字符串或嵌套的

<script>                (open tag)
    alert("<script>     (text node - contents of the script)
</script>               (close tag)
");                     (text node - plain text)

The second </script> is ignored, as there's no other <script> tag for it to close.

第二个被忽略,因为没有其他的

To work around this, break up </script so that the HTML parser doesn't see it. For instance:

要解决这个问题,可以分解

alert("<script><\/script>");

or:

或者:

alert("<script><" + "/script>");

or just put the code in an external Javascript file. This issue only arises for inline scripts.

或者把代码放在一个外部的Javascript文件中。这个问题只出现在内联脚本中。

#2


1  

it is because of the \ I believe. i have no concrete explanation since I am a newbie to Javascript but this code should work:

这是因为我相信。我没有具体的解释,因为我是Javascript的新手,但是这段代码应该可以工作:

alert("<script><\/script>");

came up with it using Java knowledge.. Haha since the \ is an escape key in many languages.

使用Java知识提出它。哈哈,因为它是许多语言中的一个转义键。

相关文章