Is there an easy way to take a string of html in JavaScript and strip out the html?
feedback
|
If you're running in a browser, then the easiest way is just to let the browser do it for you...
| |||||||||||||||||||
feedback
|
| |||||||||||||||
feedback
|
Another, admittedly less elegant solution than nickf's or Shog9's, would be to recursively walk the DOM starting at the <body> tag and append each text node.
| |||||||||||
feedback
|
Converting HTML for Plain Text emailing keeping hyperlinks (a href) intactThe above function posted by hypoxide works fine, but I was after something that would basically convert HTML created in a Web RichText editor (for example FCKEditor) and clear out all HTML but leave all the Links due the fact that I wanted both the HTML and the plain text version to aid creating the correct parts to an STMP email (both HTML and plain text). After a long time of searching Google myself and my collegues came up with this using the regex engine in Javascript:-
the str var starts out like this:-
which renders like this:- --start-- this string has html code i want to remove Now back to normal text and stuff --end--and then after the code has run it looks like this:-
As you can see the all the HTML has been removed and the Link have been persevered with the hyperlinked text is still intact. Also I have replaced the and To change the link format (eg. "BBC (Link->http://www.bbc.co.uk)" ) just edit the " $2 (Link->$1) ", where $1 is the href URL/URI and the $2 is the hyperlinked text. With the links directly in body of the plain text most SMTP Mail Clients convert these so the user has the ability to click on them. Hope you find this useful. | |||
feedback
|
I built this JavaScript library for a Konfabulator widget that does exactly that. It completely strips out comments and <style> and <script> tags and tries to be somewhat smart about converting <br/>'s and <p/>'s into newlines as well. | |||
feedback
|
I think the easiest way is to just use Regular Expressions as someone mentioned above. Although there's no reason to use a bunch of them. Try:
| |||
feedback
|
Simplest way:
That retrieves all the text inside a div. | |||||||||||||
feedback
|
I made some modifications to original Jibberboy2000 script Hope it'll be usefull for someone
| |||
Was this post useful to you?
|
I altered Jibberboy2000's answer to include more BR tag types formats, remove everything inside SCRIPT and STYLE tags, format the resulting HTML by removing multiple line breaks and spaces and convert some HTML-encoded code into normal. After some testing it appears that you can convert most of full web pages into simple text where page title and content are retained. In the simple example,
becomes
The Javascript function and test page look this:
It was used with this HTML:
| ||||
feedback
|
Define this as a jquery plugin and use it like as follows:
| |||
feedback
|