Sai*_*Sai 6 html javascript xml
我最近继承了一个庞大的webapp,它是JSP,Javascript和Java的组合.它仅适用于IE,因为它使用xml数据岛进行编码,以及阻止其他浏览器顺利运行的其他因素.一直很好,直到几天用于IE9/10的IE7/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>
Run Code Online (Sandbox Code Playgroud)
在此页面上还有另一行代码
window.dialogArguments.parent.request(underlyingdata, "CONTRACT.LIST.WB", "PULP AND PAPER|" + instrumentdd.options[instrumentdd.selectedIndex].text);
Run Code Online (Sandbox Code Playgroud)
调用如下函数
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);
}
Run Code Online (Sandbox Code Playgroud)
这在IE9/10中失败,错误是'load'不是'xmldataisland'对象上的有效方法(脚本438错误),而它在IE 5到IE 8上完全正常.
我相信上面函数中的xmldataisland对象是XMLDocument类型.为什么加载方法失败?这是什么解决方法?我从许多消息来源读到并听到使用数据岛是一个糟糕的主意.在这种情况下,什么是正确的替代方案?
从IE10开始,不再支持XML数据孤岛 - 浏览器将它们解析为HTML.Mozilla开发者网络已经发表了一篇文章,它提供了一种跨XML浏览器替代XML数据岛的方法,即HTML5"数据块".本文演示了<script>如果src省略该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>
Run Code Online (Sandbox Code Playgroud)
您需要编写自己的方法将数据加载到该块中(可能使用jQuery.get或.load方法).
希望有所帮助!