I have an html page called test.html, here is it’s content:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head>
<title></title> </head> <body style="margin: 0; padding: 0; background: #d9d9d9;">
<form name="form1" method="post" action="NewsLetterPreview.aspx?NewsletterID=1" id="form1">
<div>
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKMTA3NzM3OTc4N2Rkkk3StA9iwyEdCoR43JPpw6OyRHubNTnbXatTbxCXsVA=" />
</div>
</form> </body> </html>
I then have another page which I am using $.get to get the content of the page:
$.get("test.html", function (data) {
alert($(data).find("body").html());
});
However the alert($(data).find("body").html()) returns null. It does the same for the alert($(data).find("form").html()) also. Only if I specify alert($(data).find("div").html()) does it return the input field.
How can I select the body / form?
If you want to replace your body with fetched HTML, this should do it.
EDIT:
Missread your question.
It’d suggest you to use json or .replace() instead.
Example:
alert(data.replace(/^.*?\<body\>(.*?)\<\/body\>.*$/, "$1"));If you want body tags included:
alert(data.replace(/^.*?(\<body\>.*?\<\/body\>).*$/, "$1"));I don’t think jquery can work with HTML the way you wish. Either include it in the code and then use regular functions or find another workaround.