Search code examples
javascriptxmlgoogle-chromeinternet-explorerlegacy

Create Xml document based on the browser using Javascript


I am trying to get an older application to work in Chrome and Edge. It currently works only in Compatibility Mode. I have narrowed it down to where it gets the browser type and then creates the xml document based on the browser type.

    // Identify and return the browser type
    function getBrowserType()
    {
        try
        {
            if (typeof ActiveXObject != 'undefined')
            {
                //Microsoft Internet Explorer
                return 'MSIE';   
            }
            else if (typeof document != 'undefined'
                && document.implementation
                && document.implementation.createDocument
                && typeof DOMParser != 'undefined')
            {
                //Other browsers
                return 'OTH';    
            }
        }
        catch(ex)
        {
            alert('Unable to find browser type.\n' + ex.message);
        }
    }
//create the xml DOM by browser detection

    function createDoc(browserType)
{
     var xmlDOM = null;
        if (browserType == 'MSIE')
        {
            try 
            {
                var names = [   'Msxml2.DOMDocument.6.0',
                                'Msxml2.DOMDocument.3.0',
                                'MSXML2.DOMDocument',
                                'MSXML.DOMDocument',
                                'Microsoft.XMLDOM'      ];
                for (var key in names)
                {
                    try
                    {
                        xmlDOM = new ActiveXObject(names[key]);
                    }
                    catch(ex)
                    {}
                }
            }
            catch(ex)
            {
                alert('Unable to create XML Document.\n' + ex.message);
            }
        }
        else if (browserType == 'OTH')
        {
            try
            {
                xmlDOM = document.implementation.createDocument("", "", null);
            }
            catch(ex)
            {
                alert('Unable to create XML Document.\n' + ex.message);
            }
        }


        return xmlDOM;
    }

So the object that is created is incorrect and it will not create the xml document. If the object created is the ActiveXObject, in Compatibility Mode, it has no problem. So this is based on the browser. So I basically need a way for this to work in all browsers. I think there may be a better way to do this as this is very old 2009 code.


Solution

  • I have tested your code on my side, it seems that I can't detect the IE browser (I'm using IE 11).

    I suggest you could try to detect the browser using the window.navigator.userAgent property. code as below:

      if (window.navigator.userAgent.toLowerCase().indexOf("trident") > -1) {
         //Microsoft Internet Explorer
         return "IE";
      }
    

    Because, the Browser Agent strings as below:

    enter image description here

    [Note] The navigator data can be changed by the browser owner, if the userAgent was not modified, user could use the above method to detect the browser using JavaScript.

    Then, you could refer to this article to create XML DOM.