我编写了一个应用程序,其中 AJAX 加载一个 XSLT,其中包含 <script>
里面。
奇怪的是,脚本在 Firefox 中运行时没有 eval()
。脚本不在 Opera 或 Chrome 中运行。
因此,我简化了代码并将其发布在这里。
这是 Firefox 的错误吗?
有跨浏览器的解决方法吗? (一种解决方法,不在 Firefox 上运行 eval()
,仅适用于来自 xslt 的脚本,但在 Chrome、Opera 上为加载了 AJAX 的每个脚本运行 eval()
- 无论是在 XSLT 中还是在 XHTML 中)。
index.xhtml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head><script src="app.js" /></head>
<body onload="locationHashChanged();" />
</html>
app.js
function get(file) {
var request = new XMLHttpRequest();
request.open("GET", file, false);
request.send();
return request.responseXML;
}
function insert(where, root) {
//var scripts = root.getElementsByTagName('script');
where.parentNode.replaceChild(root, where);
//for(var z = 0; z != scripts.length; z++) eval(scripts[z]);
}
function locationHashChanged() {
var xml = get('apps.xml');
var xslt = get('xslt.xsl');
var xsltProcessor = new XSLTProcessor();
xsltProcessor.importStylesheet(xslt);
insert(document.body, xsltProcessor.transformToFragment(xml, document).firstChild);
};
xslt.xsl
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*" />
<xsl:template match="//*[@path='']">
<body xmlns="http://www.w3.org/1999/xhtml">
<script>alert('Only in Firefox');</script>
</body>
</xsl:template>
</xsl:stylesheet>
apps.xml
<?xml version="1.0" encoding="UTF-8"?>
<menu name="" path="" />
最佳答案
将警报放入 CDATA 标记会有帮助吗?所以
<script type="text/javascript">
<![CDATA[alert('Only in Firefox');]]>
</script>
我已经开始对 xslt 模板中包含的所有 javascript 执行此操作
关于javascript - 使用 AJAX 加载的 XSLT,包含仅在 Firefox 中触发的 <script>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29869327/