Extracting text (cross platform)

Discussion in 'Javascript' started by Debbie, Jul 16, 2006.

  1. Debbie

    Debbie Guest

    Is there a standard way to extract text from a web page, without using
    innertext/innerhtml?

    It's an academic exercise, and we've been advised that we can't use
    Internet Explorer DOM extensions that are not part of the W3C DOM.

    Thanks,

    Debbie
     
    Debbie, Jul 16, 2006
    #1
    1. Advertising

  2. Debbie wrote:
    > Is there a standard way to extract text from a web page, without using
    > innertext/innerhtml?
    >
    > It's an academic exercise, and we've been advised that we can't use
    > Internet Explorer DOM extensions that are not part of the W3C DOM.


    Well then use the W3C DOM, text will sit in text nodes as leaf nodes of
    the DOM tree and each text node has a property named nodeValue that will
    give you the text in the text node. You could also use the data property
    for that.
    If you want the text in an element then you will either have to go
    through the child nodes and concatenate the text of the child nodes
    (where you might have to recursively go down the tree until you have the
    text nodes) or depending on your needs and requirements you can use the
    W3C DOM Level 3 property named textContent which Mozilla has been
    supporting for quite some time and which at least Opera supports too now.
    Then there is the W3C DOM Level 2 Range API that also allows you to get
    the text in a range so you could position the range on an element node
    and call toString on the range e.g.
    var range = document.createRange();
    range.selectNodeContents(someNode);
    var text = range.toString();
    Mozilla and Opera 8 and later support the Range API.

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Jul 16, 2006
    #2
    1. Advertising

  3. Debbie

    Debbie Guest

    Thank you, Martin, that does just what I was looking for.

    Regards,

    Debbie
     
    Debbie, Jul 16, 2006
    #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. jcc
    Replies:
    15
    Views:
    4,768
    Nigel Wade
    May 12, 2006
  2. MeTheGameMakingGuy

    Cross Platform, Curses-Style Text Editor?

    MeTheGameMakingGuy, Aug 24, 2008, in forum: Python
    Replies:
    0
    Views:
    421
    MeTheGameMakingGuy
    Aug 24, 2008
  3. Tomás Ó hÉilidhe

    Cross-platform: Coloured text, Networking, Multithreading

    Tomás Ó hÉilidhe, Nov 12, 2008, in forum: C Programming
    Replies:
    34
    Views:
    981
    CBFalconer
    Nov 14, 2008
  4. Jonathan Hartley

    cross-platform coloured text in terminal

    Jonathan Hartley, Apr 16, 2010, in forum: Python
    Replies:
    4
    Views:
    387
    Jonathan Hartley
    Apr 20, 2010
  5. Brock Weaver
    Replies:
    48
    Views:
    490
    tony summerfelt
    Aug 29, 2005
Loading...

Share This Page