views:

325

answers:

3

I have a javascript/jQuery block as a callback after $.get function:

function myCallBack(data, textStatus) {
  var text1 = $(data).html();
  document.write(text1);
}

The data contains html data ok. I'd like to strip the html and get only inner html into text1 variable. For some reason it doesn't work. Firebug kinda "crashes" upon executing line 'var text1 = ...'

Edited:

My data variable contains:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "w3.org/TR/xhtml1/…;\r\n\r\n<html xmlns="w3.org/1999/xhtml">;\r\n
<head>\r\n 
<title></title>\r\n
</head>\r\n
<body>\r\n Testing...\r\n</body>\r\n
</html>\r\n

And I'd like to parse the part between body tags.

+5  A: 

You mean you want the inner text?

var text1 = $(data).text();

[Update]

Try it with this regular expression:

var bodyText = new RegExp(/<body[^>]*>([\S\s]*?)<\/body>/).exec(data)[1];
Andreas Grech
Thank you for an answer. That doesn't work for me however, it returns empty string.My data variable contains: "<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\r\n\r\n<html xmlns="http://www.w3.org/1999/xhtml">\r\n<head>\r\n <title></title>\r\n</head>\r\n<body>\r\n Testing...\r\n</body>\r\n</html>\r\n"And I'd like to parse the part between <body> and </body> tags.
Pompair
Pompair
im trying to find a solution atm for this
Andreas Grech
posted a solution with a regexp
Andreas Grech
A: 

Try this:

$(data)[1].data

But I think that just works with a specific example and not in general.

Gumbo
A: 

You perform a webrequest by get. That implies that the result will only be a string.

var text1 = data ;

Is all you can get. There is no DOM-object you can traverse. You only get this if you access elements on you own page.

Malcolm Frexner
Good point! :) Thank you.
Pompair