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

Discussion in 'Javascript' started by prashantpawar@gmail.com, Sep 2, 2005.

  1. Guest

    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, 2005
    #1
    1. Advertising

  2. 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/
     
    Martin Honnen, Sep 2, 2005
    #2
    1. Advertising

  3. Ivo Guest

    "Martin Honnen" said
    > 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|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/
     
    Ivo, Sep 2, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Erik van Duijn
    Replies:
    1
    Views:
    337
    David Dorward
    Jan 21, 2004
  2. Bartek
    Replies:
    1
    Views:
    389
    Martin Honnen
    Jul 7, 2004
  3. \A_Michigan_User\
    Replies:
    2
    Views:
    938
    \A_Michigan_User\
    Aug 21, 2006
  4. Michael Mair

    Reading whole text files

    Michael Mair, Feb 10, 2005, in forum: C Programming
    Replies:
    50
    Views:
    1,334
    Chris Croughton
    Feb 16, 2005
  5. Tomasz Wrobel
    Replies:
    1
    Views:
    112
    Robert Klemme
    Apr 30, 2009
Loading...

Share This Page