lotrss
lotrss

Reputation: 11

Is there a way to store HTML file text into an array using Javascript

I want to extract all the words from the HTML file and store them into a textarea as follows. Is there any way to grab all this content in an array using Javascript? Any tips would be much appreciated.

<textarea id="url" name="url" type="Text"></textarea>
<a href="javascript:oF(document.getElementById('url').value)">Open File</a>

<script>
var f;
function oF(url) {
    f = window.open();
    f.location = url;
}
</script>

What I need is, when I click the URL [open file] the html scripts will be stored in that textarea.

Upvotes: 0

Views: 274

Answers (1)

user3886234
user3886234

Reputation:

If I understand you, you're wanting to take HTML and extract as many words as you can out of it to get a word count. If that's so, this may be what you want:

function stripHtml(html) {
   var tmp = document.createElement("DIV");
   tmp.innerHTML = html;
   return tmp.textContent || tmp.innerText || "";
}

var str = "<p>I'm a word!! </p><p>Me too!!!!!</p>";

//Strip the HTML and remove puncuation
str = stripHtml(str).toLowerCase();
str = str.replace(/[!@#$%^&*\(\)\.?\{\}\[\]|<>,-=_+']/g,'');

//Split into an array of words
var words = str.split(/\b/g);

From there you have an array of all of the words in the document, and you just have to count them. Good luck :)

Upvotes: 1

Related Questions