最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - jQuery, XML as a String, and IE - Stack Overflow

matteradmin3PV0评论

I just inherited a project where the main navigation menu is currently Flash. They asked if I could switch it to javascript, so I agreed to give it a shot. The navigation structure itself is dynamically generated on the server and new nodes are picked via an ajax call. The return is all XML.

To prevent delays on initial load, the server sends down the xml from a first call for the current page into a textarea.

Firefox and Chrome can pull that xml and manipulate it in jQuery just fine. IE, however, chokes out. I know that IE doesn't play well if the MIME type isn't set, but as the server is essentially off limits, I need to find a way around this.

An example of the xml stored in the textarea would be something like:

<nav> <item name='Link 1' url='' img='/path/to/image.png' /> <item name='Link 2' url='' img='/path/to/image.png' /> </nav>

I am grabbing the contents using the .val() method, which works in everything other than IE. I have banged my head for awhile on this. Any help?

I just inherited a project where the main navigation menu is currently Flash. They asked if I could switch it to javascript, so I agreed to give it a shot. The navigation structure itself is dynamically generated on the server and new nodes are picked via an ajax call. The return is all XML.

To prevent delays on initial load, the server sends down the xml from a first call for the current page into a textarea.

Firefox and Chrome can pull that xml and manipulate it in jQuery just fine. IE, however, chokes out. I know that IE doesn't play well if the MIME type isn't set, but as the server is essentially off limits, I need to find a way around this.

An example of the xml stored in the textarea would be something like:

<nav> <item name='Link 1' url='http://www.somesite.' img='/path/to/image.png' /> <item name='Link 2' url='http://www.somesite.' img='/path/to/image.png' /> </nav>

I am grabbing the contents using the .val() method, which works in everything other than IE. I have banged my head for awhile on this. Any help?

Share Improve this question asked Feb 17, 2010 at 18:21 Kevin EatonKevin Eaton 1103 silver badges13 bronze badges 1
  • I should mention that I am taking the XML, iterating over the elements and any children, and then creating <ul>'s with the elements and adding it to the menu where necessary. It works great in everything other than IE and I need to get a IE workaround ASAP. Thanks again! – Kevin Eaton Commented Feb 17, 2010 at 18:32
Add a ment  | 

3 Answers 3

Reset to default 5

This issue has been resolved here

<script type="text/javascript">

$(parseXml($("#xml").val())).find('item').each(function(){
 ...

});

function parseXml(xml)
{
    if (jQuery.browser.msie)
    {
        var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.loadXML(xml);
        xml = xmlDoc;
    }
    return xml;
}

</script>

I don't know that using Javascript is a good idea in order to implement this. If anything, I would implement this as a series of nested UL/LI elements (with links inside the LI elements) and then use a third-party library like jQuery to generate the menu for you (there are a number of plugins which will create menus for you based on this structure (or a similar hierarchical structure).

This leads to better searchability on your site, since search engines won't process the ajax call to get the content, and this way, your links are embedded in the page.

Also, it requires less code on your part, since most libraries like jQuery correctly abstract out the differences in browsers when offering their functionality.

A suggestion would be to make sure you set the content type in your ajax call to "xml".

datatype: "xml"

IE need this to be specifically spelled out.

I would also suggestion using json instead of XML due to the size overhead of XML. If this is an option for you I would seriously consider it. Json is very easy to work with and is extremely fast for ajax calls.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far