我使用浏览器内的XSLT生成HTML,包括<script>
。在这种情况下,Firefox显然表现得很奇怪。
The files below can be obtained and tried from there。 (打开doc.xml并查看控制台中的日志消息。)
下面是我制作的测试XSLT样式表。 XML源doc.xml只是<?xml-stylesheet type="text/xsl" href="stylesheet.xsl"?><doc/>
。转换会创建内联脚本和外部脚本:
<html xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xsl:version="1.0">
<head>
<title>Firefox and XSLT inserted scripts</title>
</head>
<body>
<script type="text/javascript" src="script.js"></script>
<script type="text/javascript">
console.log('inline, asynchronous? '+ document.currentScript.async);
// -> "inline, asynchronous? false"
// alert('inline, pause');
function errorHandler(img) {
img.src = 'http://people.mozilla.com/~faaborg/files/shiretoko/firefoxIcon/firefox-32-noshadow.png';
}
try {
document.write("<p>Inserted</p>");
} catch(e) {
console.error(e.name + ": " + e.message);
// -> "InvalidStateError: An attempt was made to use an object that is not, or is no longer, usable"
}
</script>
<p><img src="about:blank" onError="window.errorHandler?errorHandler(this):console.log('Too late for handling error!')"/></p>
</body>
</html>
外部脚本script.js
几乎与内联脚本相同:
console.log('external, asynchronous? '+document.currentScript.async);
// -> "external, asynchronous? false"
// alert('external, pause');
try {
document.write("<p>Inserted</p>");
} catch(e) {
console.error(e.name + ": " + e.message);
// -> "InvalidStateError: An attempt was made to use an object that is not, or is no longer, usable"
}
控制台显示:
19:21:14.435 "Too late for handling error!" index.xml:1
19:21:14.477 "external, asynchronous? false" script.js:1
19:21:14.478 "InvalidStateError: An attempt was made to use an object that is not, or is no longer, usable"
script.js:7
19:21:14.479 "inline, asynchronous? false" index.xml:1
19:21:14.480 "InvalidStateError: An attempt was made to use an object that is not, or is no longer, usable"
index.xml:10
async
是假的。document.write
。errorHandler()
)无法立即使用。有没有人知道这里发生了什么?
答案 0 :(得分:3)
Mozilla的XSLT处理器实现会创建一个结果树并直接呈现,因此不支持使用document.write
,请参阅https://developer.mozilla.org/en/docs/XSL_Transformations_in_Mozilla_FAQ#What_about_document.write.3F。其他客户端XSLT处理器实现可能会将序列化转换结果提供给其HTML解析器,在这种情况下,document.write可以正常工作。
如果要在XSLT中使用脚本输出内容,则需要使用DOM方法创建节点并插入它们。有关示例,请参阅http://home.arcor.de/martin.honnen/xslt/test2013122801.xml。