码迷,mamicode.com
首页 > Web开发 > 详细

如何在编辑器里添加CSS或JS代码

时间:2015-07-30 16:23:33      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:

最近在使用Wordpress做一个网站项目,需要在后台编辑器里添加自定义的JS脚本和CSS
在编辑器里添加好后,发现在浏览器里JS脚本执行有问题并且CSS的脚本直接被WordPress保存的时候过滤掉了
经过排查发现原因是Wordpress默认的TinyMCE在从代码模式切换到可视化模式的时候对脚本时行了处理
更无耻的是CSS的脚本直接被删除了

  1. //编辑器里代码模式下的代码
  2. <script type="text/javascript">
  3. //my code....
  4. </script>
  5. //编辑器里可视化模式下的代码
  6. <script type="text/javascript">
  7. // <![CDATA[
  8. //my code....
  9. // ]]>
  10. </script>
wiki CDATA上的说明

CDATA sections in XHTML documents are liable to be parsed differently by web browsers if they render the document as HTML, since HTML parsers do not recognise the CDATA start and end markers, nor do they recognise HTML entity references such as &lt; within <script> tags. This can cause rendering problems in web browsers and can lead to cross-site scripting vulnerabilities if used to display data from untrusted sources, since the two kinds of parser will disagree on where the CDATA section ends.

Since it is useful to be able to use less-than signs (<) and ampersands (&) in web page scripts, and to a lesser extent styles, without having to remember to escape them, it is common to use CDATA markers around the text of inline <script> and <style> elements in XHTML documents. But so that the document can also be parsed by HTML parsers, which do not recognise the CDATA markers, the CDATA markers are usually commented-out, as in this JavaScript example:


所以正确的写法应该是这样子的
  1. //JavaScript example:
  2. <script type="text/javascript">
  3. //<![CDATA[
  4. //mycode
  5. document.write("<");
  6. //]]>
  7. </script>

  8. //CSS example:
  9. <style type="text/css">
  10. /*<![CDATA[*/
  11. //my code
  12. body { background-image: url("marble.png?width=300&height=300") }
  13. /*]]>*/
  14. </style>

 
参考:




如何在编辑器里添加CSS或JS代码

标签:

原文地址:http://www.cnblogs.com/huangtailang/p/93d806b642106d04f173ee15ad6bb1a0.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!