Have a link open the search function (Ctrl+F) on WordPress

I’ve looking for a way to open the browser search function to look for text on only one page of my WordPress site.
I want this to be self contained on one page. Basically the functionality of pressing Ctrl+F, but have it as a link or button, which in my boss’ words, is “for the less tech-savvy users out there.”

This is what I found so far

Read More

< a href=”#” onclick=”find();”>Click here to search the page < /a>

Which worked great on FireFox, but did nothing in Chrome.

Is there a way to have this work in other browsers or have it so when I click the link on the page, it carries out the Ctrl+F function to open the search box?

Thanks

Related posts

Leave a Reply

1 comment

  1. As far as I know, you can’t fire Chrome’s native find function without hitting cmd + F. But you can always use window.find(). Perhaps that will help you better

    EDIT:

    After some testing, I came up with this pen. It works, kinda. I tested it on Chrome, Firefox and IE. Chrome and Firefox highlight the first occurrence of the word. If you click the button fast enough, they might highlight you the second one. IE doesn’t do sh*t. Perhaps you could code different solutions for different browsers, but I don’t know how that’d fit in your current code. In any case, window.find() has a documentation, but this bug report suggests this method should be terminated.

    Another solution entirely: use Javascript’s search() or indexOf() going through your whole page (yeah I know), and based on those returns, you can hardcode something that highlights the matched area, such as wrapping it around a <span> that has yellow background. This is but a suggestion, as I have not given it much thought, but I think it will help.