Using browser search `Ctrl+F` through a button in user control
You can send commands to the hosted web control using the IOleCommandTarget. It's supposed to be equivalent to HtmlDocument.ExecCommand method but for some reason, some commands don't work in the later.
The command you're after is IDM_FIND.
Here is some C# code to run it:
private void SearchButton_Click(object sender, EventArgs e)
{
dynamic ax = webBrowser1.ActiveXInstance;
// IHtmlDocument2 also implements IOleCommandTarget
var qi = (IOleCommandTarget)ax.Document;
// MSHTML commands group
var CGID_MSHTML = new Guid("de4ba900-59ca-11cf-9592-444553540000");
const int IDM_FIND = 67;
qi.Exec(CGID_MSHTML, IDM_FIND, 0, null, null);
}
[Guid("b722bccb-4e68-101b-a2bc-00aa00404770"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IOleCommandTarget
{
void NotDefinedHere(); // don't remove this
[PreserveSig]
int Exec([MarshalAs(UnmanagedType.LPStruct)] Guid pguidCmdGroup, int nCmdID, int nCmdexecopt, object pvaIn, [In, Out] object pvaOut);
}
Possible to add search bar that functions like using (command F) or (CTRL F) inside a website
So I found this on another thread and it seems to work but it only finds the first occurrence. I need it to highlight all the occurrences. Any idea how to get it to do that?
<p> hello world, hello world, hello world, hello world</p>
<!--BEGIN SEARCH BOX -->
<div class="search_box">
<form action="" id="form2">
<div>
<input type="text" id="search">
<input type="button" id="submit_form" onclick="checkInput()" value="Submit">
</div>
</form>
</div>
<!--END SEARCH BOX -->
<script>
function checkInput() {
var query = document.getElementById('search').value;
window.find(query);
return true;
}
</script>
How to make browser page search (Ctrl-F) start in specific div
There is a way to create a selection with javascript...
Now, if you select
some text and then search
something with the find
browser tool...
The search result
must start from that point.
(even if the results show all matches in the page, the first result starts where the user has the text selection)
So, if we join this two.. we can have an approach of what you need...
something like:
$(window).on('keydown', function (event) {
if ((event.ctrlKey || event.metaKey) && (String.fromCharCode(event.which).toLowerCase() == 'f')) {
if (document.selection) {
var range = document.body.createTextRange();
range.moveToElementText(document.getElementById('search_from_here'));
range.select();
} else if (window.getSelection) {
var range = document.createRange();
range.selectNode(document.getElementById('search_from_here'));
window.getSelection().addRange(range);
}
}
});
Check this jsFiddle for an example http://jsfiddle.net/gmolop/tdo7p1o5/
Important!: The focus must be on the "result iframe" for this example to work (in jsfiddle)
Is it possible to simulate a Ctrl+F key combination using Jquery?
Some browsers support window.find()
Related Topics
Prevent Long Running JavaScript from Locking Up Browser
How to Run Node Js Code from Npm Inside of Swift
Triggering Onclick Event Using Middle Click
Why Is the Ecc-Dh Symmetric Key of This Site Different from Openssl
Firebase.Database Is Not a Function
JavaScript Search Array of Arrays
Youtube Iframe Player API - Onstatechange Not Firing
Ruby with Watir: Handling JavaScript Popup Window
Ruby Hash Equivalent of JavaScript's Object Initializer Es6 Shorthand
How to Pause Setinterval() Functions
Explanation of [].Slice.Call in JavaScript
JavaScript Parse Float Is Ignoring the Decimals After My Comma
Display Spinner While Datatables Table Loads Ruby on Rails
How to Validate a Radio Button
How to Invoke Ruby from Node.Js
Safari on iOS 9 Does Not Trigger Click Event on Hidden Input File