Local Search Function for HTA or HTML instead of Ctrl+B

From: Benny Pedersen (b.pedersen_at_get2net.dk)
Date: 03/26/04


Date: 26 Mar 2004 09:36:53 -0800

Hi

I needed a VBScript, a local search function instead of Crtl+B in HTA
or HTML.

The only working solution that I found was this JavaScript, :-),
Thanks.

  http://groups.google.ca/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&selm=a7sgis%24l5r%241%40newswriterENV1.svr.pol.co.uk

But a problem with that code is when a document is to long, then
the FIND button disappears in the top of the screen when repeating the
find button. So, for each submit, I had to scrool to the top of the
page.

Here's a solution:

<html><head><title> HIT RETURN KEY INSTEAD OF THE FIND BUTTON
</title>
<SCRIPT language="JScript">
function getFocus(){
        if(event.keyCode==13)document.mySearch.findButton.focus();
}
document.onkeypress=getFocus

var NS = (document.layers);
var IE = (document.all);
var n = 0;

function findInPage(str){
  var txt, i, found;
  if (str == "") return false;
  if (NS){
    if (!window.find(str))
      while(window.find(str, false, true)) n++;
    else
      n++; if (n==0)alert("Not found.");
  }
  if (IE){
    txt = window.document.body.createTextRange();
    for (i = 0; i <= n && (found = txt.findText(str)) != false; i++){
      txt.moveStart("character", 1);
      txt.moveEnd("textedit");
    }
    if (found){
      txt.moveStart("character", -1);
      txt.findText(str);
      txt.select();
      txt.scrollIntoView();
      n++;
    }
    else{
      if (n > 0){
        n = 0;
        findInPage(str);
      }
      else
        alert("Not found.");
    }
  }
  return false;
}
</SCRIPT>
</head>
<body onLoad="document.mySearch.string.focus()">
<form name="mySearch" onSubmit="return findInPage(this.string.value)">
<font size=3><input name="string" type="text" size=15 onChange="n=0;"
onFocus="this.value=''"></font>
<input name="findButton" type="submit" value="Find">
</form>
<p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p>

  To test our scripts, just put alot of text here.

Benny Pedersen, http://2dos.homepage.dk
</body></html>