Asynchronously load the contents of a div

I really think the usual (and cleanest) approach would be a proper backend service… it depends on the amount of data though, I suppose. If you want to do this on the client side, you might parse the AJAX response with a DOMParser – images will only get loaded when they’re actually getting appended to the DOM then. Something like

$.get('content.html', function (data) {
  var parser = new DOMParser()
  var doc  = parser.parseFromString(data, 'text/html')

  // You can now access the content using jQuery like e.g.
  $('.foo', doc).clone().appendTo(document.body)
})
1 Like