简体   繁体   English

如何在html中显示脚本标签而不执行

[英]how to display a script tag in html and not execute it

I want to display a script tag that looks like: <script.. 我想显示一个脚本标签,如下所示: <script..

if I run JavaScript 如果我运行JavaScript

encodeURI("<script")

Then output that to the browser it no longer looks like < script 然后将其输出到浏览器,它不再像< script

I'm strong it on my backend with the real less than sign but want to convert it with javascript, then display that conversion as html I assume with < 我在后端使用真正的小于号来增强它,但是想用javascript对其进行转换,然后将该转换显示为html,我假设使用<

Is there a javascript function that will do this? 有没有可以执行此操作的javascript函数? (obviously not encodeURI) (显然不是encodeURI)

thanks 谢谢

There is no native function, but you can manually create one: 没有本机函数,但是您可以手动创建一个:

function htmlEscape(str) {
    return str.replace(/</g,'&lt;').replace(/>/g,'&gt;');
}

You can manually do replace like that. 您可以像这样手动替换。

Call it like this: 这样称呼它:

htmlEscape('<script');

You must use a method or a tool which escapes some html characters. 您必须使用可以转义一些html字符的方法或工具。

For a tool please see http://www.iwantaneff.in/entifier/ for sample 有关工具,请参见http://www.iwantaneff.in/entifier/中的示例

So your code will be 所以你的代码将是

&lt;script&gt;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM