doormen2014 2011-01-20 04:51
浏览 58
已采纳

无法在<script>标记或其他内容上呈现页面或DOMDocumentFragment中断

After some trial and error, I've come to notice that my Template engine fails on template files that contain <script> tags. This is clearly a huge problem, but I can't seem to figure out why it's failing.

It's very peculiar too, the manner with which it fails. With the <script> tags commented out, the page loads normally, however with the <script> tags uncommented, the page loads, and performing a View Source shows that the entire page has loaded, though nothing is rendered in the browser. The Firebug DOM inspector, shows a head tag, and an empty body.

The stranger part (I think at least) is that these script tags are targeting external files. If there was in-page scripting outside of a CDATA section, I could understand some unexpected results.

I'd post the code used, but it's extremely long and I wouldn't know where to start, ie; where the issue is, if in fact there even is one.

In short, my template engine creates a single DOMDocument object, and then creates DOMDocumentFragment objects to manage parts of the template. The first parsing iteration typically loads the entire template file into a fragment, and then it works down the tree based on certain parameters, looking for certain tags.

TL;DR: Does anyone know why the PHP DOM classes may be failing to build the page properly due to script tags? If I enclose them in <![CDATA[ ... ]]> tags, it does nothing. Only when I remove or comment out the tags does a browser load and render the page.

  • 写回答

1条回答 默认 最新

  • dongtongjian3127 2011-01-20 09:15
    关注

    Is the content of the script tag wrapped in a CDATA block? If it isn't, the DOM will attempt to parse its contents and fail. Make sure all script code is wrapped in CDATA blocks, and that the DOM parser is configured to output CDATA blocks (otherwise the output will contain empty script tags).

    I would also advise putting as much of the script code off-page as possible and placing it in its own files that you then link to with <script src="/path/to/script.js"></script> markup. This avoids the DOM parsing problem, and has the additional benefit that the browser only needs to load the code once and can keep it cached in subsequent page views.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!