finding text on webpage

Discussion in 'Javascript' started by doug s, Sep 11, 2005.

  1. doug s

    doug s Guest

    How would I access the source of a webpage? I would like to get the source,
    then use a regexp to find text on a webpage. For example, retrieving a
    counter, or some other text that dynamically changes. I'm implementing this
    for firefox.

    right now, i have a web page opening that contains that info, but i would
    rather have a pop up alert() that shows that info.

    thanks for any help. and any code snippets would be great.
    doug s, Sep 11, 2005
    #1
    1. Advertising

  2. doug s

    RobG Guest

    doug s wrote:
    > How would I access the source of a webpage? I would like to get the source,
    > then use a regexp to find text on a webpage. For example, retrieving a
    > counter, or some other text that dynamically changes. I'm implementing this
    > for firefox.
    >
    > right now, i have a web page opening that contains that info, but i would
    > rather have a pop up alert() that shows that info.
    >
    > thanks for any help. and any code snippets would be great.
    >
    >


    There are at least three ways of getting the text content of the document.

    The standards-compliant method is to use document.body.textContent, but
    since it's DOM 3 it may not be widely supported beyond Mozilla/Firefox.

    The IE-centric way is to use innerText as IE does not support textContent.

    A third, reasonably cross-browser method is to use innerHTML and a
    regular expression. But it's not a standard and different
    implimentations may have small variations in how they've copied it from IE.

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <title> blah </title>
    <meta http-equiv="Content-Type"
    content="text/html; charset=ISO-8859-1">
    <script type="text/javascript">

    function showText( el )
    {
    var txt;
    if ( el.textContent ) {
    txt = 'textContent\n' + el.textContent;
    } else if ( el.innerText ){
    txt = 'innerText\n' + el.innerText;
    } else if ( el.innerHTML ) {
    txt = 'innerHTML\n' + el.innerHTML.replace(/<\/?.[^\>]*>/g,'');
    }
    return txt;
    }

    </script>
    </head>
    <body >
    <div onclick="alert(showText( document.body ));">
    Here is <div>the <span> content. <b>Hi</b></span>
    <br>Click me to see the content.
    </div>
    </div>
    </body>
    </html>

    --
    Rob
    RobG, Sep 12, 2005
    #2
    1. Advertising

  3. doug s

    doug s Guest

    "RobG" <> wrote in message
    news:jY2Ve.455$...
    > There are at least three ways of getting the text content of the document.
    >
    > The standards-compliant method is to use document.body.textContent, but
    > since it's DOM 3 it may not be widely supported beyond Mozilla/Firefox.
    >
    > The IE-centric way is to use innerText as IE does not support textContent.
    >
    > A third, reasonably cross-browser method is to use innerHTML and a regular
    > expression. But it's not a standard and different implimentations may
    > have small variations in how they've copied it from IE.
    >
    > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    > "http://www.w3.org/TR/html4/strict.dtd">
    > <html>
    > <head>
    > <title> blah </title>
    > <meta http-equiv="Content-Type"
    > content="text/html; charset=ISO-8859-1">
    > <script type="text/javascript">
    >
    > function showText( el )
    > {
    > var txt;
    > if ( el.textContent ) {
    > txt = 'textContent\n' + el.textContent;
    > } else if ( el.innerText ){
    > txt = 'innerText\n' + el.innerText;
    > } else if ( el.innerHTML ) {
    > txt = 'innerHTML\n' + el.innerHTML.replace(/<\/?.[^\>]*>/g,'');
    > }
    > return txt;
    > }
    >
    > </script>
    > </head>
    > <body >
    > <div onclick="alert(showText( document.body ));">
    > Here is <div>the <span> content. <b>Hi</b></span>
    > <br>Click me to see the content.
    > </div>
    > </div>
    > </body>
    > </html>
    >
    > --
    > Rob


    thanks. that helps. Is there a way to fetch a page, without actually opening
    it, and then use showText(el) ? Somehow pass in the url ?

    thanks again.
    doug s, Sep 12, 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. Replies:
    1
    Views:
    394
  2. Paul
    Replies:
    14
    Views:
    817
    Alexey Smirnov
    Jun 19, 2008
  3. stig
    Replies:
    0
    Views:
    75
  4. sifar
    Replies:
    5
    Views:
    379
  5. soren625
    Replies:
    2
    Views:
    349
    soren625
    Dec 12, 2006
Loading...

Share This Page