</script>在字符串结尾脚本部分 [英] </script> in string ends script section

查看:87
本文介绍了</script>在字符串结尾脚本部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试使用JQuery将字符串追加到div时遇到问题.

I'm having an issue where I'm trying to use JQuery to append a string to a div.

该字符串为HTML,有时用户会在字符串中使用脚本标签.

This string will be HTML, and occassionally users will be using the script tags inside the string.

不幸的是,当浏览器在字符串中读取</script>时,它结束了该节,并将以下所有javascript输出到浏览器中.显然,我们不想要那样.

Unfortunately, when the browser reads </script> inside the string, it ends the section and prints out all the following javascript into the browser. Obviously, we don't want that.

是否可以让浏览器不解析字符串中的任何内容?

Is there a way to get the browser to not parse anything inside the string?

例如,可以将Adsense广告附加到div.

An example of this might be an Adsense ad being appended to the div.

推荐答案

您可以使用反斜杠(\)来使<script> close标记中的斜杠转义,从而不会关闭实际的<script>元素:

You can use a backslash (\) to escape the slash in the <script> close tag so that it doesn't close the actual <script> element:

var foo = document.write('<script src="somewhere.js"><\/script>');

当从具有本地后备功能的CDN加载脚本时,通常使用此方法:

This is often used when loading a script from a CDN with a local fallback:

<script src="//cdn.example.com/jquery.js"></script>
<script>
    if (!window.jQuery) document.write('<script src="/assets/js/jquery.js"><\/script>');
</script>

这篇关于&lt;/script&gt;在字符串结尾脚本部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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