有没有办法检测在JavaScript页面搜索中查找 [英] Is there a way to detect find on the page searches in javascript

查看:85
本文介绍了有没有办法检测在JavaScript页面搜索中查找的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每个浏览器都有页面功能(ctrl + F)。有没有办法检测javascript中的用户搜索,以便我可以附加其他操作。

解决方案

当然可以尝试 hook ctrl + f cmd + f 快捷方式,一些浏览器,这样你只会知道用户按下该快捷方式,最有可能搜索某些东西。



如果浏览器允许覆盖该快捷方式,您可以进一步阻止默认行为,并在站点上实现自己的搜索逻辑。但是,这被认为是非常糟糕的做法。覆盖原生浏览器行为非常糟糕。



另一方面,当浏览器执行搜索过程时,没有任何事件被触发。简而言之,不,实际上没有办法通过javascript来检测或者插入搜索过程(如果有的话,它将永远不会与浏览器兼容)。


each browser has find on page functionality (ctrl+F). Is there a way to detect user searches in javascript so that I could attach additional actions.

解决方案

Of course you can try to hook into ctrl+f or cmd+f shortcut, but even if that works on "some" browsers, that way you only would know that an user pressed that shortcut and is most likely to search for something.

If the browser allows to overwrite that shortcut, you could further block the default behavior and implement your own search logic on the site. But that is considered most times as very bad practice. Overwritting native browser behavior is pretty pretty bad.

On the other hand, there is no "event" which gets triggered when a browser executed a search process. In short words, no, there actually is no way to detect or hook into a search process with javascript (if there is one, it's never gonna be cross browser compatible).

这篇关于有没有办法检测在JavaScript页面搜索中查找的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆