Cross-browser: detect blur event on window

后端 未结 6 2356
挽巷
挽巷 2021-02-08 03:24

I just read, I think all the thread that deals with this subject, and I can\'t find a real solution to my problem. I need to detect when the browser window loses its focus, i.e.

6条回答
  •  渐次进展
    2021-02-08 03:44

    The document.hasFocus (MDN) is an implementation that can resolve the problem with Firefox, but in Opera it isn't supported. So, a combined approach can reach out the problem you are facing.

    The function below exemplifies how can you use this method:

    function getDocumentFocus() {
        return document.hasFocus();
    }
    

    Since your question isn't clear enough about the application (timed, pub/sub system, event driven, etc), you can use the function above in several ways.

    For example, a timed verification can be like the one implemented on this fiddle (JSFiddle).

提交回复
热议问题