开发者

Can IE manipulate XML using jQuery?

开发者 https://www.devze.com 2023-02-12 06:21 出处:网络
What I am not trying to do: Simply \"read\" XML in IE using jQuery. Been there, done that. Works for the most part.

What I am not trying to do:

  • Simply "read" XML in IE using jQuery. Been there, done that. Works for the most part.
  • Load XML via AJAX. This is a legacy system using XML in a hidden field (oh yah, baby!) between postbacks to store a wizard data structure. Rewriting it would suck.

What I am trying to do:

  • Manipulate the XML document using jQuery in IE
  • Use the same code across all browsers, using the native jQuery functionality

What I would be okay with:

  • Overriding/overloading the same jquery methods to get them to work in IE when manipulating the XML DOM.

It just doesn't work and I feel like it just isn't possible in a 100% cross browser way using plain old jQuery methods.

Case in point:

<!DOCTYPE html>
<html>
<head>
    <title>IE Sucks</title>
    <script src="Scripts/jquery-1.5.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        var xml =
            '<Browsers>' +
                '<CoolBrowsers>' +
                    '<Browser name="Opera"></Browser>' +
                    '<Browser name="Chrome"></Browser>' +
                    '<Browser name="Firefox"></Browser>' +
                '</CoolBrowsers>' +
                '<BadBrowsers>' +
                    '<Browser name="IE6"></Browser>' +
                '</BadBrowsers>' +
            '</Browsers>';

        $(function () {

            $("#xml").text(xml);

            var uncoolBrowser = $("<Browser />").attr("name", "IE7");

            // In 1.5, using this...
            var $xml = $($.parseXML(xml));

            // Nope. Works everywhere else, though!
            // var $xml = $(xml);     


            // Throws a "Type mismatch"
            // Works everywhere except IE
            // This is case sensitive (??? WTF ???)
            // Lowercase "badbrowsers" nothing happens
            // Uppercase "BADBROWSERS" nothing happens
            // Best part? $xml.find("BadBrowsers").length === 1
            $xml.find("BadBrowsers").append(uncoolBrowser);

            // Only way to output XML in IE
            $("#result").text($xml[0].xml);

            // Fuggetaboutit
            // Technically, it does work in IE but not when using $.parseXML()
            // $("#result").text($("<div></div>").append($xml.clone()).html());
        });
    </script>
</head>
<body>
    <pre id="xml"></pre>
    <pre id="result"></pre>
</body>
</html>

Is it possible? Can this simple scenario be done or has IE just forsaken us all? $(xml).everything, etc. works in FF, Opera, Chrome, and Safari.

Update

It is possible using voodoo magicks.

I've created a jQuery plugin that takes care of reconciling the differences between different browser handling of XML. I also made an .xml() function based on similar code elsewhere, though mine fixes an IE-only issue. This works in all browsers, IE7 & IE8 for sure, can't test IE6.

I have posted this on my github. If anyone has suggestions or improvements, let me k开发者_运维技巧now. There are several things I've already run into but I have been fixing them as I run into them.


This is more of a guess as I don't know offhand what .parseXml does but IE needs createElement for unknown node names. Can you try document.createElement('BadBrowsers') for every new node you are going to manipulate?

This is the case with HTML5 and that's why there are shiv scripts. You can try just taking this:

http://html5shiv.googlecode.com/svn/trunk/html5.js

Copying it, append your new node names to var z, and then:

<!--[if lt IE 9]>
<script src="file.js"></script>
<![endif]-->
0

精彩评论

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