开发者

Javascript/XSL which works in Firefox does nothing Safari

I have some pretty basic Javascript here which works just fine in Firefox but when rendered in Safari, does nothing at all.

<html>
<head>
    <script type="text/javascript">
        function XsltTransform(xmlfile, xslfile) {
            var xml = document.implementation.createDocument("", "", null);
            var xslt = document.implementation.createDocument("", "", null);

            xml.async = false;
            xslt.async = false;

            xml.load(xmlfile);
            xslt.load(xslfile);

            var processor = new XSLTProcessor();
            processor.importStylesheet(xslt);

            var XmlDom = processor.transformToDocument(xml)

            var serializer = new XMLSerializer();
            var output = serializer.serializeToString(XmlDom.documentElement);

            var outputDiv = document.getElementById("contentbody");
            outputDiv.innerHTML = output;
        }
    </script>
</head>
<body>
    <div id="main">
        <div id="menu">
            <div id="categorytitle">
                Categories</div>
            <ul id="categorybody">
                <li><a href="javascript:void(0)" onclick="XsltTransform('category.xml','category.xslt');">Latest news</a></li>
            </ul>
        </div>
        <div id="content">
            <div id="contenttitle">
                Content</div>
            <div id="contentbody">
                Body
            </div>
        </div>
    </div>
</body>
</html>

The javascript error says that the onclick call is calling an unknown function: XsltTransform. But why? Am I simply using the wrong standard Javascript for Safari. FYI, this is aimed at working in Mobile Safari.

I've not included the XML and XSL files because they are proprietary and really they work fine on their own in other browsers. Also, the error is coming from the Javascript conso开发者_运维知识库le.

Error:

TypeError: 'undefined' is not a function (evaluating 'xml.load(xmlfile)')

I assume this means that 'load' is undefined. Is this correct and if so, what do I do about it? AJAX, I'm sure, does work in Safari!

Thanks,

Matt.


Mozilla supports a load method on XML DOM documents, I think WebKit does not. Thus to load an XML document for Safari you should consider to use XMLHttpRequest and then after a successful request you can use the responseXML property. So you need to make two XMLHttpRequests, one for the XML document, the other for the stylesheet, then you have two responseXML documents, pass the one for the stylesheet to the importStylesheet method, the other one to the transformation method you use.

I would also suggest to use transformToFragment(xml, document) instead of serializing the transformation result to then parse it again by setting innerHTML.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜