0

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.

2
  • Did you try innerHTML? Commented Jul 6, 2009 at 9:12
  • Just tried. var text1 = data.innerHTML; Returns "undefined". Commented Jul 6, 2009 at 9:59

3 Answers 3

6

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];
Sign up to request clarification or add additional context in comments.

4 Comments

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" "w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\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> and </body> tags.
Thanks for an answer. with find&html() returns null and with find&text() returns "". (an empty string)
im trying to find a solution atm for this
posted a solution with a regexp
0

Try this:

$(data)[1].data

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.