Loading html and manipulate it's DOM before inserting it into the document
Hi folks, I've some problems loading a complete validated HTML5-File (with <!DOCTYPE html> & <head> & <body>) from a given
url, and manipulate it's content (respectively select a specific
source-element in it) before inserting it into a
target-element in the displayed document.
-
loadContent = function(url, sourceElement, targetElemet) {
-
var ajaxParams = {};
- ajaxParams.url = url;
-
ajaxParams.dataType = "html";
-
ajaxParams.success = function(returnedData) {
-
var newDocument = $(returnedData);
-
// does not work. no manipulations seems to work
- // even $(newDocument).html() is null.
-
// $(newDocument).find("head").remove();
- // clear target-element and insert loaded document
-
$(targetElemet).children().remove();
-
$(targetElemet).html("");
-
$(targetElemet).append(newDocument);
-
// this works, but before this, the document is not valid:
- // <head>-element in the middle of the document
-
$(targetElemet).find("head").remove();
-
};
-
$.ajax(ajaxParams);
- };
$(newDocument).find("head") does not work like I think it should work, and I can not select a specific element in the loaded document, i. e. just the body.
I know the function .load() allows to select specific elements of the loaded document, but i need control over all callbacks, not only on success.
The question is: how can i generate a complete detached DOM-Tree from a loaded url and manipulate it before we insert it into the shown document?