jQuery load body from external HTML

Diogo Cardoso picture Diogo Cardoso · Mar 11, 2011 · Viewed 22.3k times · Source

I'm using jQuery and trying to load the body from an external HTML file.

It works if I try to load some div:

$('body').load('example.html #content');

But I'm unable to do something like this:

$('body').load('example.html body');

Answer

Adam Hopkinson picture Adam Hopkinson · Mar 11, 2011

From http://api.jquery.com/load

jQuery uses the browser's .innerHTML property to parse the retrieved document and insert it into the current document. During this process, browsers often filter elements from the document such as <html>, <title>, or <head> elements. As a result, the elements retrieved by .load() may not be exactly the same as if the document were retrieved directly by the browser.

Reading the above, it seems possible that your browser be only returning the body innerHTML. This obviously should be the same as requesting body, but maybe it's causing an error because body is not found?

I'd suggest trying a different browser.