我最近继承了一个巨大的webapp,它是JSP、Javascript和Java的组合。它仅适用于 IE,因为它使用 xml 数据岛和其他阻止在其他浏览器上顺利运行的东西进行编码的方式。一切都很好,直到几天后,为少数用户推出了 Windows 7 盒子,其中 IE9/10 在应用程序中的某些 javascript 出现问题。例如,下面的数据岛是我的 html 页面的一个片段。
<xml id = "underlyingdata" ondatasetcomplete="window.dialogArguments.parent.repopulateDropDown(this, underlyingdd)">
</xml>
<xml id="termdata" ondatasetcomplete="window.dialogArguments.parent.repopulateDropDown(this, termdd)">
</xml>
在这个页面上还有一行代码
window.dialogArguments.parent.request(underlyingdata, "CONTRACT.LIST.WB", "PULP AND PAPER|" + instrumentdd.options[instrumentdd.selectedIndex].text);
调用函数如下
function request(xmldataisland, requestmethod, parameters
{
var screwcache=Math.round(Math.random()*10000);
xmldataisland.value=null;
xmldataisland.load("/webaccess/Request?sessionid=" + sessionid + "&request=" + requestmethod + "¶meters=" + parameters+"&screwcache="+screwcache);
}
这在 IE9/10 中失败,错误是“load”不是“xmldataisland”对象上的有效方法(脚本 438 错误),而它在 IE 5 到 IE 8 上运行良好。
我相信上述函数中的 xmldataisland 对象属于 XMLDocument 类型。为什么加载方法失败?解决方法是什么?我从许多来源读到并听说使用数据孤岛是一个糟糕的主意。在这种情况下,正确的替代方案是什么?
最佳答案
从 IE10 开始,XML data islands are no longer supported - 浏览器将它们解析为 HTML。 Mozilla 开发者网络已经制作了一个 article它为 XML 数据岛提供了跨浏览器的替代方案,即 HTML5“数据 block ”。文章演示了 <script>
如果 src
元素可以用作数据 block 属性被省略,type
属性不指定可执行脚本类型。您还必须确保嵌入的 XML 内容不包含 </script>
标签。
来源:https://developer.mozilla.org/en/docs/Using_XML_Data_Islands_in_Mozilla
这是他们给出的 HTML 示例...
<!DOCTYPE html>
<html>
<head>
<title>XML Data Block Demo</title>
<!-- this is the data block which contains the XML data -->
<script id="purchase-order" type="application/xml">
<purchaseOrder xmlns="http://example.mozilla.org/PurchaseOrderML">
<lineItem>
<name>Line Item 1</name>
<price>1.25</price>
</lineItem>
<lineItem>
<name>Line Item 2</name>
<price>2.48</price>
</lineItem>
</purchaseOrder>
</script>
<script>
function runDemo() {
// the raw XML data can be retrieved using this...
var orderSource = document.getElementById("purchase-order").textContent;
// the XML data can be parsed into a DOM tree using the DOMParser API...
var parser = new DOMParser();
var doc = parser.parseFromString(orderSource, "application/xml");
var lineItems = doc.getElementsByTagNameNS("http://example.mozilla.org/PurchaseOrderML", "lineItem");
var firstPrice = lineItems[0].getElementsByTagNameNS("http://example.mozilla.org/PurchaseOrderML", "price")[0].textContent;
document.body.textContent = "The purchase order contains " + lineItems.length + " line items. The price of the first line item is " + firstPrice + ".";
}
</script>
</head>
<body onload="runDemo()";>
Demo did not run
</body>
</html>
您需要编写自己的方法来将数据加载到该 block 中(可能使用 jQuery.get
或 .load
方法)。
希望对您有所帮助!
关于javascript - xml 数据孤岛的解决方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18748571/