469,306 Members | 1,982 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,306 developers. It's quick & easy.

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

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.

Sep 2 '05 #1
2 1806

pr***********@gmail.com wrote:
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.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Sep 2 '05 #2
Ivo
"Martin Honnen" said
pr***********@gmail.com wrote:
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.


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|s mall|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/a...erties/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/

Sep 2 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Bartek | last post: by
4 posts views Thread by KenH | last post: by
4 posts views Thread by E | last post: by
60 posts views Thread by marss | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.