0

I have 3 files, (htm, xml, js) to work together fine in IE8, but in IE10 the javascript cannot get xml element:

getxml.htm

<html>
    <head><title>getxml.htm</title></head>

    <xml id="myxml" src="myxml.xml"></xml>
    <script src="getxml.js" language="javascript" type="text/javascript"></script>
</html>

myxml.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<publish>
</publish>

getxml.js

get_xml_content();
function get_xml_content() {
    alert("get_xml_content");
    alert(myxml);       // ie8: [object], ie10: [object HTMLUnknownElement]
    var xmle=myxml.getElementsByTagName("publish").item(0);  
    alert(xmle);        // ie8: [object], ie10: null
}

Alert message from IE8 and IE10 are written as comments above in getxml.js.

Appreciate any help, thanks!

3
  • What is myxml? I've never heard of embedding XML documents in HTML like that. Commented Dec 14, 2012 at 0:50
  • @Blender Probably because it appears to only work in old ie Commented Dec 14, 2012 at 0:53
  • @Blender, This is legacy code, I am trying to make it work on IE10 Commented Dec 14, 2012 at 6:39

1 Answer 1

2

According to a blog post at the IEBlog, IE10 does not support XML Data Islands when running in Standards mode. This brings IE10 into conformance with how other browsers parse HTML.

To get this to work in IE10, you need to load the page in legacy mode. You can do that by including the X-UA-Compatible meta tag:

<meta http-equiv="X-UA-Compatible" content="IE=9" />

A more cross-browser approach would be to link the XML with an <iframe> instead:

<iframe id="myxml" src="myxml.xml" />

Of course, you'll need to hide it with a bit of CSS:

iframe { display: none; }

Reference contentDocument to parse the document:

var myxml = document.getElementById("myxml").contentDocument;
var xmle=myxml.getElementsByTagName("publish").item(0);  
alert(xmle);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. I tried it but I found the content of xml is showing on html now in a frame...
@EthanLong - Hide it with CSS.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.