退出脚本标记内容 [英] Escape </ in script tag contents

查看:124
本文介绍了退出脚本标记内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在HTML中,标签和实体不在< script> 标签内,< / 立即结束标签。因此,

 < script>< b> fun& amp; amp; amp; amp;事情< / 

会为您提供一个脚本标记,其中包含< b> fun& amp; amp; amp; amp; amp; amp;如果你包含JSON,并且你想包含字符< / $ c>,那么你可以用< \ / 替换它,因为这些字符只出现在字符串中,并且<$ c $



然而,如果你不使用JavaScript,那么,这个把戏不起作用。在我的例子中,我试图在源文件中插入< script type =math / tex> ,以便 MathJax 会处理它。有没有办法在原始HTML源代码中转义< / ? (我对< / >没有特别的需求,但是我正在编写一个通用工具,并希望能够使用任何文本。)



(可以在JavaScript中创建脚本标记并填充它的 innerText ,但我正在处理原始HTML,所以我)在HTML中,与XHTML相反,一个的内容与HTML相反,脚本元素被处理为纯文本,除了出现一个结束标记,因此< / 结束处理,并且必须在符合文档的情况下,开始结束标记< / script> 。没有通用的机制来避免这种情况。任何绕过这个特征的方法都不可避免地依赖于元素内使用的语言。 语言这个词在这里被引用,因为内容可以是任何东西,只要你的代码可以解析和处理它。



所以:没有一般的机制,但对于JavaScript以外的内容或某些浏览器识别的少数其他客户端脚本语言,您可以制定自己的规则。


In HTML, tags and entities aren't parsed within <script> tags, and </ immediately ends the tag. Thus,

<script><b>fun &amp; things</

will give you a script tag with the exact contents <b>fun &amp; things.

If you're including JSON and you want to include the characters </ in your script, then you can replace it with <\/ because the only place for those characters to appear is in a string, and \/ is an escape sequence that turns into a single forward slash.

However, if you're not using JavaScript, then this trick doesn't work. In my case specifically I'm trying to insert a <script type="math/tex"> into the source so that MathJax will process it. Is there a way to escape </ in the original HTML source? (I don't have a particular need for </ but I'm writing a generic tool and want to make it possible to use any text.)

(It's possible to create the script tag in JavaScript and populate its innerText, but I'm working with the raw HTML so I can't do that.)

解决方案

In HTML, as opposite to XHTML, the content of a script element is processed as plain text except for the occurrence of an end tag, so that </ ends processing and must, in conforming documents, start the end tag </script>. There is no general mechanism to avoid this. Any methods that circumvent this feature are unavoidably dependent on the "language" used inside the element. The word "language" is in quotes here, because the content can be just about anything, as long as your code can parse and process it.

So: no general mechanism, but for content other than JavaScript or some of the few other client-side scripting languages recognized by some browsers, you can make your own rules.

这篇关于退出脚本标记内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆