JSF 2.2 CDATA被转义 [英] JSF 2.2 CDATA is escaped

查看:183
本文介绍了JSF 2.2 CDATA被转义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

升级到Mojarra 2.2.3后,我发现了一些奇怪的行为.以下Javascript声明被弄乱了:

I have found some weird behaviour after upgrading to Mojarra 2.2.3. The following Javascript declaration gets mangled:

在.xhtml文件中:

In the .xhtml file:

<script type="text/javascript">
<!-- /* <![CDATA[ */
    $(document).ready(function() {                                                                             
        if ($('#some_identifier').size() > 0) 
        ...
/* ]]> */-->
</script>

这被弄成以下废话:

<script type="text/javascript">
<!-- /* &lt;![CDATA[ */
    $(document).ready(function() {
        if ($('#some_identifier').size() &gt; 0)
        ...
/* ]]&gt; */-->
</script>

这会破坏嵌入在.xhtml文件中的所有javascript代码.我验证了我们以前使用的版本(2.0.x)不会发生这种情况,因此我必须假定它与新的Mojarra版本有关.关于如何解决或解决此问题的任何想法?

This breaks all javascript code embedded in .xhtml files. I verified that this does not happen with the versions we used previously (2.0.x), so I must assume it's got something to do with the new Mojarra version. Any ideas on how to fix this or work around it?

推荐答案

此CDATA语法完全无效.目前尚不清楚您从何处获得此信息,以及为什么认为此信息有效.也许您将其与CSS代码的CDATA语法混淆了.无论如何,对于JS代码中正确的CDATA语法,请仔细阅读以下Mozilla开发人员网络文章:编写JavaScript XHTML .

This CDATA syntax is completely invalid. It's not clear where you got this from and why you thought it would be valid. Perhaps you confused it with CDATA syntax for CSS code. In any case, for proper CDATA syntax in JS code, carefully read this Mozilla Developer Network article: Writing JavaScript for XHTML.

有效(现代)语法为:

<script type="text/javascript">
  <![CDATA[
    $(document).ready(function() {                                                                             
      if ($('#some_identifier').size() > 0) 
        ...
  ]]>
</script>

如果您确实确实需要支持世界上没有人使用的旧版浏览器,请使用以下语法,这些语法应在那些本机不支持JavaScript并因此无法解析<script>元素的浏览器中使用(想知道自己,您的JSF应用程序(包含丰富的JavaScript)是否仍可以在那些网络浏览器上运行?对他们来说也摆脱JS真的有用吗?)

If you really, really need to support old browsers which no one on the world uses, then use the following syntax which should work in those browsers which doesn't natively support JavaScript and therefore are incapable of parsing <script> elements (wonder yourself, would your JSF application, rich of JavaScript, still work on those webbrowsers? would it really be useful to escape JS for them as well?)

<script type="text/javascript">
  <!--//--><![CDATA[//><!--
    $(document).ready(function() {                                                                             
      if ($('#some_identifier').size() > 0) 
        ...
  //--><!]]>
</script>

顺便说一句, 更好的是将JS代码放在自己的.js文件中.

By the way, much better is to put that JS code in its own .js file.

这篇关于JSF 2.2 CDATA被转义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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