Reputation: 221997
I have a stupid problem. An jQuery.ajax
request return me a full HTML text as a string. I receive such response in an case of error on the server. The server give me an error description which I want to place inside of the corresponding place of my current page.
So now the question: I have a string contains full HTML document (which is not an XML!!! see <hr>
element inside). I need to have for example only BODY part as a jQuery object. Then I could append it to the corresponding part of my page.
Here is an example of the string which I need to parse:
<html>
<head>
<title>The resource cannot be found.</title>
<style>
body {font-family:"Verdana";font-weight:normal;font-size: .7em;color:black;}
p {font-family:"Verdana";font-weight:normal;color:black;margin-top: -5px}
// ...
</style>
</head>
<body bgcolor="white">
<span><H1>Server Error in '/' Application.<hr width=100% size=1 color=silver></H1>
<h2> <i>The resource cannot be found.</i> </h2></span>
<font face="Arial, Helvetica, Geneva, SunSans-Regular, sans-serif ">
<b> Description: </b>HTTP 404. The resource you are looking for ...bla bla....
<br><br>
<b> Requested URL: </b>/ImportBPImagesInfos/Repository.svc/GetFullProfilimageSw<br><br>
<hr width=100% size=1 color=silver>
<b>Version Information:</b> Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.1
</font>
</body>
</html>
<!--
[HttpException]: A public action method '....
at System.Web.Mvc.Controller.HandleUnknownAction(String actionName)
at System.Web.Mvc.Controller.ExecuteCore()
at System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext)
at System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext)
at System.Web.Mvc.MvcHandler.<>c__DisplayClass8.<BeginProcessRequest>b__4()
at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass1.<MakeVoidDelegate>b__0()
at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.End()
at System.Web.Mvc.Async.AsyncResultWrapper.End[TResult](IAsyncResult asyncResult, Object tag)
at System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag)
at System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult)
at System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result)
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
-->
Upvotes: 10
Views: 21260
Reputation: 11
i did it with using DOMParser by fortunated accident after triying some of this post answers, here is my code:
const extract_body = (html_string) => {
// asuming html_content contains just one body element
let parser = new DOMParser();
let dom_document = parser.parseFromString(html_string, "text/html");
let body_element = dom_document.getElementsByTagName("body")[0];
console.log(body_element.innerHTML);
}
Upvotes: 1
Reputation: 38046
And the must-have non-jQuery answer:
var bodyHtml = /<body.*?>([\s\S]*)<\/body>/.exec(entirePageHTML)[1];
This will return only whats inside the body tags.
UPDATE this accepts the attributes set on the body tag
Upvotes: 20
Reputation: 31883
Another way to do this, without jQuery:
function getStupidErrorMessage(str) {
var bodyTags = str.match(/<\/*body[^>]*>/gim);
// returns an array
// bodyTags[0] is body open, bodyTags[1] is body close
// unless someone output the markup backwards :)
bodyContents = str.slice(bodyTags[0].length,-(bodyTags[1].length));
return bodyContents; // use as innerHTML of <body>
}
If you need the attributes of the BODY tag, parse those as well.
Upvotes: 4