function evalHTML(value) {
if (typeof(value) != 'string') {
return null;
}
value = MochiKit.Format.strip(value);
if (value.length == 0) {
return null;
}
var parser = MochiKit.DOM.DIV();
var html = MochiKit.DOM.currentDocument().createDocumentFragment();
var child;
parser.innerHTML = value;
while ((child = parser.firstChild)) {
html.appendChild(child)
}
return html
}
function evalHTMLRequest(req) {
return evalHTML(req.responseText);
}
evalHTML takes a string and returns a DocumentFragment? object representing the HTML Nodes suitable of inserting via MochiKit or regular DOM, Works in IE, FireFox? and Safari
example:
MochiKit.DOM.appendChildNodes(document.body, evalHTML('<p>Hello World</p>'))
evalHTMLRequest is useful combined with MochiKit.Async.doSimpleXMLHttpRequest to load html fragments or pages
With an html fragment available at http://www.example.com/frag.html can be retrieved easily with the below code
var d = doSimpleXMLHttpRequest("http://www.example.com/frag.html")
d.addCallback(evalHTMLRequest);
d.addCallback(function(html) {
//do something with the html nodes here
});
