0

Using this post, I'm trying to load document via ajax and find contents of specific document node(s) so that I can display them without re-navigating browser.

However, my document always seems to be an empty document.

Ajax callback:

function processRatingToken(data) {  //Data is just standart HTML document string
  var doc = document.implementation.createHTMLDocument();
  doc.open();
  //Replace scripts
  data = data.replace(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi, "");
  //Write HTML to the new document
  doc.write(data);
  doc.close();

  console.log(doc.body);  //Empty
}

So what's wrong?

Note: I'm using this strategy, because I'm building a Greasemonkey Userscript. If you are developing an Ajax application, this strategy is NOT recomended. Use JSON instead.

Community
  • 1
  • 1

1 Answers1

0

There is a workaround with .innerHTML property:

doc.childNodes[1].innerHTML = data;

Where .childNodes[1] is the <html> element.