Connecting Tech Pros Worldwide Forums | Help | Site Map

Javascript: createElementNS not work in IE ?

Newbie
 
Join Date: Mar 2007
Location: china
Posts: 24
#1: Mar 8 '07
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!

sumittyagi's Avatar
Expert
 
Join Date: Mar 2007
Location: New Delhi, India
Posts: 198
#2: Mar 8 '07

re: Javascript: createElementNS not work in IE ?


Quote:

Originally Posted by mentor

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.  
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,750
#3: Mar 8 '07

re: Javascript: createElementNS not work in IE ?


createElementNS does exist. IE doesn't support it because it can't deal with XHTML properly. Use createElement instead for IE. See this link.
Newbie
 
Join Date: Mar 2007
Location: china
Posts: 24
#4: Mar 8 '07

re: Javascript: createElementNS not work in IE ?


thank you all ! I got it.
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,750
#5: Mar 8 '07

re: Javascript: createElementNS not work in IE ?


No problem. You're welcome.
Reply