browser - Tracking the use of Ctrl + F by JavaScript? - Stack Overflow

Is it possible to know what my users are searching for on my web pages by Ctrl+F in JavaScript? So when

Is it possible to know what my users are searching for on my web pages by Ctrl+F in JavaScript? So when a user uses Ctrl+F to search for something, JavaScript can capture this action (and the search phrase) and send it back to the server.

Possible? How?

Is it possible to know what my users are searching for on my web pages by Ctrl+F in JavaScript? So when a user uses Ctrl+F to search for something, JavaScript can capture this action (and the search phrase) and send it back to the server.

Possible? How?

Share Improve this question edited Jan 31, 2019 at 8:06 Fabrizio 8,0636 gold badges57 silver badges115 bronze badges asked Jan 7, 2013 at 0:13 datasn.iodatasn.io 12.9k28 gold badges122 silver badges158 bronze badges 4
  • Yes, it is possible. What specifically are you having trouble with? – Will C. Commented Jan 7, 2013 at 0:19
  • I don't know how. I searched in Google it's all triggering Ctrl + F key rather than to track it. – datasn.io Commented Jan 7, 2013 at 0:20
  • 1 found via google: stackoverflow./questions/93695/… (replace S keycode with F). This can track the keystroke - tracking what they type in the search box is out of javascripts ability. – jbabey Commented Jan 7, 2013 at 0:21
  • Sorry, I can only go so far as getting the ctrl+f key strokes. Retrieving the text they are searching for in the browser is not possible. – Will C. Commented Jan 7, 2013 at 0:39
Add a ment  | 

3 Answers 3

Reset to default 1

Nope, not possible. On some browsers you can catch the key bination Ctrl+F, but you can't spy on what the user searched for. On other browsers you can't catch Ctrl+F at all.

If it's any consolation, there's probably a security flaw you can use on IE6.

I've just found a workaround that might help in some cases.

Notice, that document and scrollable elements are always scrolling to the next searched occurrence - we can take advantage of that and create an "hidden" scrollable element and wait for it to scroll. When it scrolls you can then read the position to which it scrolled and get matched word.

http://jsfiddle/oz6gum90/6/

HTML:

<div id="hiddenScrollableContent">
    <div></div> <!-- first element must be empty -->
    <div>car</div>
    <div>cat</div>
    ...
</div>

CSS:

#hiddenScrollableContent{
    overflow: scroll;
    position:absolute;
    height:20px;  /* can't be zero ! */
    opacity:0.01; /* can't be zero ! */
    pointer-events: none;
}
#hiddenScrollableContent div{
    height: 100px;
}

JS:

$('#hiddenScrollableContent').scroll(function(e){
    var top = $(this).scrollTop();

    if (top==0)
        return; //prevents endless loop

    var index = Math.round(top / 100);
    var element = $('div', this).eq(index);
    var elementsText = element.text();
    $(this).scrollTop(0);

    console.log('top:'+top, 'index:'+index);

    $('span').text(elementsText);
});

No. You can't and by all security reasons should never have access to browser's UI elements outside page. You can capture Ctrl+F and handle it on your own though. But, of course, it will look different than browser's own UI element.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745405880a4626311.html

相关推荐

  • browser - Tracking the use of Ctrl + F by JavaScript? - Stack Overflow

    Is it possible to know what my users are searching for on my web pages by Ctrl+F in JavaScript? So when

    9小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信