Marek
Marek

Reputation: 2597

Extract text from ajaxed body using jquery

I am trying to extract text from body downloaded via ajax.

I can not inject this new body into iframe or into one of my elements because scripts within could break my page.

What I hoped will fork for me is:

$.ajax({ type: "GET",
  dataType: "text", /* this will avoid evaluating scripts */
  url: href,
  success: function (data) {
    var body = data.split('<body').pop().split('</body>')[0];
    if (body) {
      body = '<body' + body + '</body>';
      var pageText = $(body).find("style").remove().end()
          .find("script").remove().end()
          .find("noscript").remove().end()
          .text().replace(/\s{2,}/gi, " ").toLowerCase();
      if (pageText.length > 0)
          console.log(pageText);
});

I have tried placing downloaded body into DIV element because jQuery ignores BODY, replacing find.remove with detach but with not much of success.

Is there any standard solution?

Thanks

Upvotes: 1

Views: 2213

Answers (2)

red-X
red-X

Reputation: 5128

jQuery does not ignore body, see here.

does this not work?

$.ajax({ type: "GET",
  dataType: "text", /* this will avoid evaluating scripts */
  url: href,
  success: function (data) {
    var $data = $(data);
    var newHTML = $("body", $data).html();
    $("body").html(newHTML);  
  }
});

Upvotes: 0

Marko Dumic
Marko Dumic

Reputation: 9888

jQuery.load() does it all for you (removes scripts and optionally captures only the desired fragment).

E.g.

$('#result').load('ajax/test.html #container');

Loads contents of element with id container from url ajax/test.html into an element (on current page) with id result.

Upvotes: 1

Related Questions