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

"CTRL-F" w/ JavaScript? 2

Status
Not open for further replies.

link9

Programmer
Nov 28, 2000
3,387
US
Hello all --

Is there a way to bring up the find dialog box w/ javascript? The one that you get in IE when you press "CTRL-F"?

For instance, document.print(); does the same thing is "CTRL-P", and so I'm hoping there's a "find" equivalent, as well.

Thanks for any insight.

-paul

penny.gif
penny.gif

The answer to getting answered -- faq855-2992
 
The following is an IE (at least) solution. It's not exactly what I wanted, but I'm getting there. It finds the correct text, and then scrolls the page to the text. Now I want to highlight it. Anybody know a quickie?:

function findText(){
var oRange = document.body.createTextRange();
var sBookMark = oRange.getBookmark();
if(oRange.findText(document.theForm.searchText.value))
{
oRange.moveToBookmark(sBookMark);
oRange.select();
document.body.scrollTop = oRange.offsetTop;
}
else
{
alert('Not Found');
}

}

oRange.select() is the offending line that right now highlights the entire page.

-p

penny.gif
penny.gif

The answer to getting answered -- faq855-2992
 
like so?

<script type=&quot;text/javascript&quot;>
function find(word) {
var oRange = self.oRange ? self.oRange : document.body.createTextRange();
self.oRange = oRange;

// do forward search (1), whole words only (2)
if (oRange.findText(word, 1, 2)) {
oRange.select();
// collapse the range, then move it to include only the text after this word for subsequent searches
oRange.collapse();
oRange.moveStart(&quot;word&quot;, 1);
oRange.moveEnd(&quot;textedit&quot;, 1);
}
else alert(&quot;\&quot;&quot; + word + &quot;\&quot; not found.&quot;);
}
</script>



=========================================================
try { succeed(); } catch(E) { tryAgain(); }
-jeff
 
Yessir. Just exactly like that.

:)

Many thanks.

penny.gif
penny.gif

The answer to getting answered -- faq855-2992
 
I have a function that does very similar. However, it always jumps back to the top of the page. So you get a scroll-down, scroll-up result.

I'm using IE 6.0 - could that be the issue?

I trigger the &quot;search&quot; from a link near the top of the page - could that have something to do with it?

Here is my code (I like yours better, but provide it for comparison):
Code:
var range = &quot;null&quot;
function find()
{
  if (range != &quot;null&quot;)
  {
    range.execCommand(&quot;RemoveFormat&quot;)
  }
  var srch = prompt(&quot;Enter a word or phrase to seach for:&quot;,&quot;sample&quot;)
  range = document.body.createTextRange()
  if (srch && range.findText(srch))
  {
    range.expand(&quot;sentence&quot;)
    range.scrollIntoView()
    range.execCommand(&quot;ForeColor&quot;,&quot;false&quot;,&quot;orange&quot;)
  }
}

Also, why don't you use the .scrolIntoView() method?

Einstein47
(&quot;As a cure for worrying, work is better than whiskey.&quot; - Thomas Edison)
 
if you're having problems with the screen scrolling back to the top, you probably need to add &quot;return false&quot; after the function call to cancel the normal link navigation behavior:

<a href=&quot;#&quot; onclick=&quot;find();return false;&quot;>find</a>


i don't use scrollIntoView() because it seems to be unnecessary in my experience...the screen scrolls automatically for me if the match is off screen.



=========================================================
try { succeed(); } catch(E) { tryAgain(); }
-jeff
 
quite brilliant - the return false is what was needed, and I didn't even show you the link code that called the find().

Star for you!

Einstein47
(&quot;As a cure for worrying, work is better than whiskey.&quot; - Thomas Edison)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top