English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Is it possible to have JavaScript scan an entire HTML document for text, and replace that text once it's found? If so, what code would do it?

2006-06-13 11:09:30 · 2 answers · asked by Eric 2 in Computers & Internet Programming & Design

2 answers

Yes, that's possible. Javascript allows you to walk through the whole DOM (DOM stands for Document Object Model) tree of the document.

This function will do it in Firefox:

function getDocumentText() {
// Create a NodeIterator to find text
var textIterator = document.createTreeWalker(document, // Traverse entire document
NodeFilter.SHOW_TEXT,
null,
false);

// Use the iterator to loop through all text and do something with it
var text;
while((text = textIterator.nextNode()) != null) {
// do something with text.nodeValue
}
}

For another example (that will also work in IE), see: http://www.oreilly.com/catalog/jscript4/chapter/ch17.html (look for example 17-2 in particular)

2006-06-13 11:28:37 · answer #1 · answered by mikos 3 · 0 0

No because Javascript does not have file io.
It also lacks access the the document source that it is resident in.

You do have access to certain elements within the document as long as they are defined before the Javascript is executed.

2006-06-13 18:15:21 · answer #2 · answered by AnalProgrammer 7 · 0 0

fedest.com, questions and answers