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


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, :-),


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

Here's a solution:

<SCRIPT language="JScript">
function 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++;
      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);
    if (found){
      txt.moveStart("character", -1);
      if (n > 0){
        n = 0;
        alert("Not found.");
  return false;
<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;"
<input name="findButton" type="submit" value="Find">

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

Benny Pedersen, http://2dos.homepage.dk

Relevant Pages

  • Two Password Problems
    ... I'd try to use JavaScript to prompt for a password. ... The Java script I inserted is: ... var p=confirm("This page is for Governors only and it password protected, ...
  • Re: [PHP] A two flavored post
    ... I asked this question on the javascript list, ... var someLinkHref = document.getElementById.href; ... what I needed to be in the href string. ...
  • Re: Writing date and time to a document.
    ... ;-) The script does exactly what ... JavaScript guide & reference aren't bad either: ... The language attribute is deprecated, ... var timeNow = new Date; ...
  • small script --> huge load --> error message
    ... I'm sure everybody who works with javascript has seen this ... A script on this page is causing mozilla to run slowly. ... way to tell a javascript to give control back to the agent interface ... var oTable, oTHead, oTBody, oTFoot, oCaption; ...
  • Re: assign prototype to event
    ... JavaScript nowadays? ... var snowflakes = 100; ... while (t!= snowflakes) { ... mentioned a snowflake script. ...