472,119 Members | 2,116 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,119 software developers and data experts.

Javascript: createElementNS not work in IE ?

24
The following code catch an exception in IE, with number -2146827850

Expand|Select|Wrap|Line Numbers
  1.             var xmldoc=XmlDocument.create("1str","2str");
  2.             xmldoc.documentElement.setAttribute('code',code);
  3.             var node;
  4.             try{
  5.                  node=xmldoc.createElementNS('urn', 'condition');
  6.             }catch(e)
  7.             { }
It seems that IE dosn't implement createElementNS function. Is there any way to work around it?

I need your help!
Mar 8 '07 #1
4 9592
sumittyagi
202 Expert 100+
The following code catch an exception in IE, with number -2146827850

Expand|Select|Wrap|Line Numbers
  1.             var xmldoc=XmlDocument.create("1str","2str");
  2.             xmldoc.documentElement.setAttribute('code',code);
  3.             var node;
  4.             try{
  5.                  node=xmldoc.createElementNS('urn', 'condition');
  6.             }catch(e)
  7.             { }
It seems that IE dosn't implement createElementNS function. Is there any way to work around it?

I need your help!

I don't know what you are talking about. I havn't heard about createElementNS function. But if you want to parse xml text, then refer to the code below. Hope it helps.

Expand|Select|Wrap|Line Numbers
  1. 1    /**
  2. 2     * Create a new Document object. If no arguments are specified,
  3. 3     * the document will be empty. If a root tag is specified, the document
  4. 4     * will contain that single root tag. If the root tag has a namespace
  5. 5     * prefix, the second argument must specify the URL that identifies the
  6. 6     * namespace.
  7. 7     */ 
  8. 8    XML.newDocument = function(rootTagName, namespaceURL) { 
  9. 9      if (!rootTagName) rootTagName = ""; 
  10. 10      if (!namespaceURL) namespaceURL = ""; 
  11. 11      if (document.implementation && document.implementation.createDocument) { 
  12. 12        // This is the W3C standard way to do it 
  13. 13        return document.implementation.createDocument(namespaceURL, rootTagName, null); 
  14. 14      } 
  15. 15      else { // This is the IE way to do it 
  16. 16        // Create an empty document as an ActiveX object 
  17. 17        // If there is no root element, this is all we have to do 
  18. 18        var doc = new ActiveXObject("MSXML2.DOMDocument"); 
  19. 19        // If there is a root tag, initialize the document 
  20. 20        if (rootTagName) { 
  21. 21          // Look for a namespace prefix 
  22. 22          var prefix = ""; 
  23. 23          var tagname = rootTagName; 
  24. 24          var p = rootTagName.indexOf(':'); 
  25. 25          if (p != -1) { 
  26. 26            prefix = rootTagName.substring(0, p); 
  27. 27            tagname = rootTagName.substring(p+1); 
  28. 28          } 
  29. 29          // If we have a namespace, we must have a namespace prefix 
  30. 30          // If we don't have a namespace, we discard any prefix 
  31. 31          if (namespaceURL) { 
  32. 32            if (!prefix) prefix = "a0"; // What Firefox uses 
  33. 33          } 
  34. 34          else prefix = ""; 
  35. 35          // Create the root element (with optional namespace) as a 
  36. 36          // string of text 
  37. 37          var text = "<" + (prefix?(prefix+":"):"") +  tagname + 
  38. 38              (namespaceURL 
  39. 39               ?(" xmlns:" + prefix + '="' + namespaceURL +'"') 
  40. 40               :"") + 
  41. 41              "/>"; 
  42. 42          // And parse that text into the empty document 
  43. 43          doc.loadXML(text); 
  44. 44        } 
  45. 45        return doc; 
  46. 46      } 
  47. 47    };
  48.  
Mar 8 '07 #2
acoder
16,027 Expert Mod 8TB
createElementNS does exist. IE doesn't support it because it can't deal with XHTML properly. Use createElement instead for IE. See this link.
Mar 8 '07 #3
mentor
24
thank you all ! I got it.
Mar 8 '07 #4
acoder
16,027 Expert Mod 8TB
No problem. You're welcome.
Mar 8 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

6 posts views Thread by Mike Kamermans | last post: by
reply views Thread by Nicolas VanOrton | last post: by
136 posts views Thread by Matt Kruse | last post: by
3 posts views Thread by BakedBean | last post: by
4 posts views Thread by E | last post: by
4 posts views Thread by praskuma | last post: by
2 posts views Thread by Holger Jeromin | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.