Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

search html for a string

Status
Not open for further replies.

dbartlett41

Programmer
Mar 21, 2006
9
CA
i want to put some java script in a page that searches the html in the same page for a string. how can you do this? how can you store the document to a string or somehow scan through its own document looking for a certain line of html and if found store it to a variable along with everything else following it for one line??
 
ok so what command stores it to a string so i can search it?
 
To find all the HTML code inside the body, you can use this:

Code:
var myCode = document.getElementsByTagName('body')[0].innerHTML;

And then you can search myCode however you wish to.

Hope this helps,
Dan

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 

<html>
<head>
<script language="JavaScript">
function init() {
var found = document.body.innerHTML.indexOf("Nested");
alert(found);
}
</script>
</head>

<body onload="init()">
<div>
<p>
Nested element!
</p>
</div>
</body>
</html>


will this do the same thing ?
is your code looking for the body tag ans starting there and storeing everything after that to the variable?? and is mine storeing everything after the word nested??
 
is your code looking for the body tag ans starting there and storeing everything after that to the variable?

No. It does what I said - it will give you all the HTML code [!]within the body[/!]. This is not necessarily the same as all the HTML code [!]after[/!] the body.

Dan

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
ok sounds good so how would i search rfor an item multiple times say the word "hi" and increase a count everytime it is found? i know how the .search("")works but is there another way besides finding the search term and shortening the string every time the term is found?? possibly a built in function i dont know about
 
The "indexOf" function takes 2 parameters, the second of which is optional.

The first parameter is the string to search for, and the second is the position to start the search from.

Hope this helps,
Dan



[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
i see thanks that helps eliminate a bit of code. thanks again for you help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top