Work with AJAX response with DOM methods

Posted by Stomped on Stack Overflow See other posts from Stack Overflow or by Stomped
Published on 2010-06-03T19:38:07Z Indexed on 2010/06/03 20:34 UTC
Read the original article Hit count: 202

Filed under:
|

I'm retrieving an entire HTML document via AJAX - and that works fine. But I need to extract certain parts of that document and do things with them.

Using a framework (jquery, mootools, etc) is not an option.

The only solution I can think of is to grab the body of the HTML document with a regex (yes, I know, terrible) ie. <body>(.*)</body> put that into the current page's DOM in a hidden element, and work with it from there.

Is there an easier/better way?

Update

I've done some testing, and inserting an entire HTML document into a created element behaves a bit differently across browsers I've tested. For example:

  • FF3.5: keeps the contents of the HEAD and BODY tags
  • IE7 / Safari4: Only includes what's between ...
  • Opera 10.10: Keeps HEAD and everything inside it, Keeps contents of BODY

The behavior of IE7 and Safari are ideal, but different browsers are doing this differently. Since I'm loading a predetermined HTML document I think I'm going to use the regEx to grab what I want and insert it into a DOM element - unless someone has other suggestions.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about dom