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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

User agents

Status
Not open for further replies.

rcusr

Technical User
Sep 4, 2002
26
0
0
GB
Hi all,

Is it possible to create a user agent in JavaScript. i.e. something that would cycle through the flat page content to check for certain snippets of code?

If so how would I go about setting something like that up?
Thanks,


Richard
 
Probably easier to open the file in notepad and use the search facility. But otherwise it's fairly easy to check for one string inside another:
Code:
<script>
function findcode(codeToFind){
  codeToFind=codeToFind.toLowerCase()
  var searchThisString = document.documentElement.innerHTML.toLowerCase();
  var numOccurrences = 0;
  var currPos = 0;
  while(currPos >= 0){
    currPos = searchThisString.indexOf(codeToFind, currPos);
    if(currPos >= 0){
      numOccurrences++;
    }
  }
  alert('Found ' + numOccurrences + ' of the string &quot;' + codeToFind);
}
 
Hi,

Thnaks for the reply... I've set up the script but I think I may have done something wrong as it maxs out my CPU and crashes my IE. Here's the pages code:


<html>
<head>
<title>Find Something</title>
<meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=iso-8859-1&quot;>
<script language=&quot;JavaScript&quot;>
function findcode(codeToFind){
codeToFind=codeToFind.toLowerCase();
var searchThisString = document.documentElement.innerHTML.toLowerCase();
var numOccurrences = 0;
var currPos = 0;
while(currPos >= 0){
currPos = searchThisString.indexOf(codeToFind, currPos);
if(currPos >= 0){
numOccurrences++;
}
}
alert('Found ' + numOccurrences + ' of the string &quot;' + codeToFind);
}

</script>
</head>

<body>
<table width=&quot;100%&quot; border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot;>
<tr>
<td><table width=&quot;100%&quot; border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot;>
<tr>
<td><a href=&quot;#&quot; onClick=&quot;findcode('table')&quot;>look</a></td>
</tr>
<tr>
<td>&nbsp;</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>&nbsp;</td>
</tr>
</table>



</body>
</html>

+++++++++++++++++++
Can you see what I've done wrong?
Thanks,

Richard
 
More what I did wrong to begin with... The currPos variable never gets set high enough to find the second occurence of the search string. It just keeps looking for it in the last place it found it.

This one works:
Code:
<html>
<head>
<title>Find Something</title>
<meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=iso-8859-1&quot;>
<script language=&quot;JavaScript&quot;>
function findcode(codeToFind){
  codeToFind=codeToFind.toLowerCase();
  var searchThisString = document.documentElement.innerHTML.toLowerCase();
  var numOccurrences = 0;
  var currPos = 0;
  var result = 0;
  while(result >= 0){
    result = searchThisString.indexOf(codeToFind, currPos);
	currPos = result + 1;
	alert(currPos);
    if(currPos >= 0){
      numOccurrences++;
    }
	
  }
  alert('Found ' + numOccurrences + ' of the string &quot;' + codeToFind);
}

</script>
</head>

<body>
<table width=&quot;100%&quot; border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot;>
  <tr>
    <td><table width=&quot;100%&quot; border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot;>
  <tr>
    <td><a href=&quot;#&quot; onClick=&quot;findcode('table')&quot;>look</a></td>
  </tr>
  <tr>
    <td> </td>
  </tr>
</table>
</td>
  </tr>
  <tr>
    <td> </td>
  </tr>
</table>



</body>
</html>
 
That works perfectly... thanks very much!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top