0

如果用户查看您的网页并按 ctrl+f (chrome) 然后在您的网页中搜索一个单词,是否有办法用 javascript 截取这个搜索词?

我想知道这些信息主要用于分析,我不需要修改搜索的行为。

谢谢

4

1 回答 1

3

Hmm, this is cool. I'd never thought about doing that.

Sadly you can't though. You might be able to capture the search string with a plugin, but seeing as this is for analytics it wont get you the result you want.

I tested this in Chrome, and when you press ctrl+f you will see the ctrl press, but not f. So you can't even detect when someone searches.

You can detect ctrl+f. Use keydown instead of keypress. I found it here:

Stop browser from auto scrolling when searching document (ctrl + f)

window.addEventListener("keydown",function (e) {
    if (e.keyCode === 114 /* F3 */ || (e.ctrlKey && e.keyCode === 70) /* ctrl+f*/ ) { 
        e.preventDefault();
    }
});
于 2013-09-11T15:28:45.570 回答