469,622 Members | 2,159 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

finding text on webpage

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.
Sep 11 '05 #1
2 3087
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
Sep 11 '05 #2

"RobG" <rg***@iinet.net.au> wrote in message
news:jY*****************@news.optus.net.au...
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.


Sep 12 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

18 posts views Thread by Michael Skind | last post: by
2 posts views Thread by Hello | last post: by
8 posts views Thread by nescio | last post: by
2 posts views Thread by ElkGroveR | last post: by
3 posts views Thread by Sharon | last post: by
4 posts views Thread by Finn Stampe Mikkelsen | last post: by
15 posts views Thread by rhino | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.