本文介绍了符合XHTML的document.write的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何以这种

格式制作符合XHTML的表达形式:


document.write("< scr" + ipt type =''text / javascript''src =''path / to / file.js''>" +"< / scr" +" ipt>");


结果证明这是一项非常重要的练习。插入''& lt;''和

''& gt;''会导致浏览器将文本写入文字

text而不是预期的脚本元素。使用转义码

似乎有效(使其符合标准)但文字不是写在页面上的



关键是要有一个条件分支,当某个

条件为真时,将生成一个脚本标记,它将调用一个

JS include来响应特定条件。


此过程适用于当前实现 - 但未通过XHTML

一致性测试。现在,我们想要两者。


谢谢。


mp


-

Michael Powe Naugatuck CT USA


''除非我们批准你的想法,否则不允许,也不会允许b $ b。' - Hilary Rosen,RIAA总裁

How can I make an XHTML-compliant form of an expression in this
format:

document.write("<scr"+"ipt type=''text/javascript'' src=''path/to/file.js''>"+"</scr"+"ipt>");

this turns out to be a non-trivial exercise. inserting ''&lt;'' and
''&gt;'' causes the browser to write the text to the page as literal
text rather than as the intended script element. Using escape codes
seemed to work (makes it standard compliant) but the text is not
written to the page.

The point is to have a conditional branch in which, when a certain
condition is true, a script tag will be generated that will call a
JS include that responds to the specific condition.

This procedure works in the present implementation -- but fails XHTML
compliance testing. Now, we would like to have both.

Thanks.

mp

--
Michael Powemi*****@trollope.orgNaugatuck CT USA

''Unless we approve your idea, it will not be permitted, it will not be
allowed.'' -- Hilary Rosen, President, RIAA

推荐答案




尝试修复它时没有什么意义,只要你做到这一点

它将无法通过用户测试,因为document.write只会在当前的XHTML UA中失败。


Jim。



There''s little point in attempting to fix it, when as soon as you do
it will fail user testing as the document.write will simply fail in
current XHTML UA''s.

Jim.





有一点用处XHML,这种格式没有使用

任何地方,并且在很少使用它的地方,它被用作text / html

,因此无论如何都被解析为HTML.


Jim Ley已经指出障碍物与您的特定情况相关

情况。


然而,没有人有权管理您的个人欲望:-)如果您是

决定使你的代码XML有效(尽管仍然没有使用

XHTML)你可以在混合环境中使用组合(你不知道

)如果它将被解析为HTML或XML)。我们在XSLT中将它用于

绑定:


< script type =" text / javascript"> / *<![CDATA [ * /

//你的代码在这里

/ *]]> * /< / script>


如果解析为HTML,它会看到两个/*...*/注释,而lt / gt标志

无论如何都不会破坏解析。


if解析为XML,它会看到/ ** /节点值和CDATA块将隐藏

所有lt / gt。


有点难看,但是防弹。



There is a little use to care of XHML, this format is not used
anywhere, and in few places where it is used it is served as text/html
and thus parsed as trached up HTML anyway.

Jim Ley already pointed on obstacles relevant to your particular
situation.

Yet no one is in power to regulate your personal desires :-) If you are
determined to make your code XML valid (despite still not working in
XHTML) you can use a combo for mixed environments (where you don''t know
in advance if it will be parsed as HTML or XML). We are using it for
bindings in XSLT:

<script type="text/javascript">/*<![CDATA[*/
// your code goes here
/*]]>*/</script>

if parsed as HTML, it will see two /*...*/ comments, and lt/gt signs
wil not break the parsing anyway.

if parsed as XML, it will see /**/ node value and CDATA block will hide
all lt/gt.

A bit ugly looking, but bulletproof.





Document.write是xhtml 1.1中的禁忌因为它不会验证为

xml并且可能会导致某些页面失败。如果您正确地将xhtml 1.1页面

作为application / xhtml + xml提供,那么该页面将由

可以处理它的浏览器解析为xml,并且xml错误最少通常

给你一个错误信息,而不是页面或页面只是

根本不工作。在真正的xhtml中,需要检查从服务器

下载的所有内容是否存在xml错误。 document.write实际上是一个

关闭的框,它可以包含各种xml错误,例如未关闭的

标签,下载后打开该框。 xml中允许的这些惊喜不是
,并且只能在xml设备中引起各种问题。

我发现处理document.write问题的最简单方法是

在服务器上使用php脚本,这样document.write会做什么

在服务器上完成然后你下载结果代码

xhtml意识到浏览器可以检查。当然IE6,可能是IE7,可以

不能处理真正的mhtml作为正确的mime类型,你只需要获得

错误消息而不显示页面。您必须在html 4.01严格中提供备用

页面,例如,对于IE6。当然,如果您将xhtml 1.1页面错误地视为html而不是真正的xhtml,那么页面

也将在IE上运行,但是为什么还要在xhtml中使用xhtml如果你这样做的话,首先是

。只有一个版本的xhtml 1.1(非常

严格),并且一些适用于xhtml 1.0

过渡的技巧将不起作用。



Document.write is taboo in xhtml 1.1 because it will not validate as
xml and may cause some pages to fail. If you serve the xhtml 1.1 page
correctly as application/xhtml+xml, the page is parsed as xml by
browsers that can handle it, and the least little xml error usually
gives you an error message instead of the page or the page just does
not work at all. In true xhtml, everything downloaded from the server
needs to be checked for xml errors. The document.write is, in effect, a
closed box that could contain all sorts of xml errors, such as unclosed
tags, once the box is opened after download. Such surprises are not
allowed in xml and can cause all sorts of problems in xml only devices.
I find the most simple way to handle the document.write problem is to
use php script on the server so that what the document.write would do
is done on the server and you then download the resulting code that the
xhtml aware browser can examine. Of course IE6, and probably IE7, can
not handle true xhtml served as the correct mime type, and you just get
error messages and no page displayed. You have to provide an alternate
page in html 4.01 strict, for example, for IE6. Of course if you serve
the xhtml 1.1 page incorrectly as html rather than true xhtml, the page
will also work on IE, but then why bother to use xhtml in the first
place if you do this. There is only one version of xhtml 1.1 (very
strict), and some of the tricks that would work on xhtml 1.0
transitional will not work on it.


这篇关于符合XHTML的document.write的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 15:19