How do I process the whole text(non-id) in javascript?

P

prashantpawar

I want to know that how can I process the whole text of the HTML
document using just the javascript(for example I wanna convert the
whole text as well as HTML to Uppercase).

I know one way of doing it by document.getElementById().innerHTML, but
that will work only when you gave id to all the text in the file.

So tell me some way by which I can access the whole text of html file
using just javascript.
 
M

Martin Honnen

I know one way of doing it by document.getElementById().innerHTML, but
that will work only when you gave id to all the text in the file.

IE allows you to serialize the current DOM of any element with
element.outerHTML so you could serialize the root element with
document.documentElement.outerHTML
There could be nodes like DOCTYPE declarations or comments outside of
the root element however which the above does not cover. And it is a
serialization of the DOM tree and not the source as read from a URL or
local file.
Some other browsers like Opera implement outerHTML as well, others like
Mozilla do not although implementing a serialization with script itself
is possible of course as the DOM tree is there.
 
I

Ivo

"Martin Honnen" said
implementing a serialization with script itself
is possible of course as the DOM tree is there.

Such as:

function gettext( o ) {
o = o || document.body;
var b, s = '', j = o && o.firstChild, phrase =
/^(abbr|b|big|cite|code|em|font|i|kbd|span|s|samp|small|strong|tt|u)$/i;
if( j && o.childNodes.length<500 ) {
for ( ; j !== null; j = j.nextSibling ) {
if( j.nodeType === 3 ) {
s += j.nodeValue || '';
}
else if( j.nodeType === 1 ) {
b = phrase.test( j.tagName ) ? '' : ' ';
s += b + gettext( j ) + b;
}
}
}
return s.replace( /\s+/, ' ' );
}

Other approaches are certainly more efficient, but this is perhaps still
readable. More about nodetypes for example here:
<URL:
http://msdn.microsoft.com/workshop/author/dhtml/reference/properties/nodetyp
e.asp > (watch for wrap)
Some effort is made to keep space characters around some phrase elements
with the rather voluminous regex in variable "phrase". It 's not the most
elegant part of the script, but prevents words running together.

hth
ivo
http://4umi.com/web/javascript/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top