Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How get <body> element from the html which one have as a string

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>&nbsp;Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.1

    </font>

  </body>
</html>
<!--
[HttpException]: A public action method &#39;....
   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)
-->
like image 399
Oleg Avatar asked May 18 '10 12:05

Oleg


People also ask

What does the body element contain in HTML?

The <body> element contains all the contents of an HTML document, such as headings, paragraphs, images, hyperlinks, tables, lists, etc.

Can you have a body inside a body HTML?

It could be about your page staying the same when the embedded page goes down or changes, who knows. In that case, you would indeed get "body inside body" in your HTML, from copying the body you want to embed from the foreign page's HTML into your own page's HTML.

Where is the body in HTML?

The HTML body element is the container for the content of an HTML document. It must be the second element of the <html> element and follows the head element. Each HTML document can only contain one body element.


2 Answers

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

like image 98
Sean Kinsey Avatar answered Oct 23 '22 00:10

Sean Kinsey


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.

like image 4
Robusto Avatar answered Oct 23 '22 00:10

Robusto