内嵌JavaScript与“< script>”字符串错误地关闭脚本标记 [英] Inline javascript with "<script>" string closes script tag by mistake

查看:109
本文介绍了内嵌JavaScript与“< script>”字符串错误地关闭脚本标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在内联一个大型JS程序,其中包含一行代码:

  doc.write(< ; script> var app = \+ _2d()+\;< / script>); 

不幸的是,浏览器(chrome)认为字符串中的脚本是关闭脚本标记,所有的东西都像HTML文本一样。



如何包含这样一个字符串并将其转义,以免混淆浏览器的HTML解析?

$ b

解决方案

我通过分割脚本标记来解决它,比如建议:

  doc.write(< scr+ipt> var app = \+ _2d()+\;< / scr+ipt>); 


I'm inlining a large JS program, which includes a line of code like :

doc.write("<script>var app = \"" + _2d() + "\";</script>");

Unfortunately the browser (chrome) thinks the script in the string is the closing script tag, and actually takes everything after that like its HTML text.

How do I include such a string and escape it so it does not confuse the browser HTML parsing?

解决方案

I solved it by splitting the script tag like this SO question recommends:

doc.write("<scr"+"ipt>var app = \"" + _2d() + "\";</scr"+"ipt>");

这篇关于内嵌JavaScript与“&lt; script&gt;”字符串错误地关闭脚本标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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