Question

I want to remove the xmlns attribute from the root html element of an XHTML document using JavaScript, but nothing I have tried seems to work:

document.documentElement.removeAttribute("xmlns")

and

document.documentElement.removeAttributeNode(document.documentElement.getAttributeNode("xmlns"))

have failed utterly to remove the xmlns attribute.

Any suggestions?

EDIT: I found that removing the xmlns on a normal document works fine, but removing it from the contentDocument of an iframe is what is giving me trouble.

Was it helpful?

Solution

Your DOM document in the iframe is clearly an XMLDocument, not an HTMLDocument.

When you remove the xmlns attribute, you change the DOM document such that it is unserializable as XML.

"outerHTML" is a serialization operation, so to write the DOM out as XML, it needs to be repaired first, which restores the attribute to the XML.

You have two options. Implement your own serializer of the DOM that repairs the output in a different way, or remove the attribute from the XML after serialization as Mr Lister suggests in the comments.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top