Extract text from ajaxed body using jquery
I am trying to extract text from body downloaded via ajax.
I can not inject this new bod开发者_如何学Pythony 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
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
.
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);
}
});
精彩评论