XMLSerializer for Internet Explorer

While trying to convert a jQuery element object into a string, I noticed that all the major browsers support “XMLSerializer”, which does precisely that task. Of course, Internet Explorer is the exception. However, IE does offer the “outerHTML” property on DOM elements, which seems to do the same thing.

I herewith present an extremely short JavaScript snippet which allows global use of XMLSerializer

if (!window["XMLSerializer"]) {
    
    window.XMLSerializer = function() {
        
        this.serializeToString = function(element) {
            return element.outerHTML;
        }
        
    }
    
}