在xhtml + xml页面中替换document.write() [英] Replacing document.write()s in an xhtml+xml page

查看:138
本文介绍了在xhtml + xml页面中替换document.write()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为一家公司工作,该公司为客户端网站嵌入了< p>脚本语言=JavaScriptsrc = .....等等。我们很依赖document.write来将元素写入页面。我们的客户之一出于某种原因选择使用内容类型application / xhtml + xml,这使得document.write()在chrome中无法使用。



I理解这是为什么,并且符合DOM规范的代码应该创建每个元素,设置其属性,如果需要,可以用文本节点填充它,将文本节点附加到其父元素和父元素到某个页面元素....



但是不需要所有这些垃圾的好解决方法是什么?其中的write()有很多元素,以至于如果我们制作节点并像Knex或Legos一样将它们固定在一起,或者您已经拥有了什么,那么结果代码将变得可怕。



编辑:尝试使用CDATA,但即使这行也被xhtml解析器谴责,与脚本嵌入页面相同:

 < script language =text / javascript><![CDATA [document.write('hi');]]>< / script> ; 


解决方案

  var el = document.createElement('div'); 
el.innerHTML ='您用于document.write()'的内容;
document.body.appendChild(el);

请注意,您需要将HTML修正为有效的XHTML,但这应该少得多工作比转换所有代码来使用DOM操作。


I work for a company that writes software which client sites embed with < script language="JavaScript" src=..... etc. etc. We depend quite a bit on document.write to write elements out to the page. One of our clients for some reason has opted to use the content-type "application/xhtml+xml", which makes document.write() unusable in chrome.

I understand why this is, and that DOM-compliant code should create each element, set its attributes, fill it with a text node if needed, attach the text node to its parent and the parent to some page element....

but what's a good workaround that doesn't require all this junk? The write()s therein have so many elements that the resulting code would be hideous if we made nodes and fastened them together like Knex or Legos or what-have-you.

edit: Tried using CDATA, but even this line is condemned similarly by the xhtml parser on the same page as our script embed:

<script language="text/javascript"><![CDATA[document.write('hi');]]></script>

解决方案

var el = document.createElement('div');
el.innerHTML = 'What you used to document.write()';
document.body.appendChild(el);

Note that you'll need to fix the HTML to be valid XHTML, but that should be much less work than converting all the code to use DOM manipulation.

这篇关于在xhtml + xml页面中替换document.write()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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