简体   繁体   English

Window.onblur function 在 Chrome 中不工作,但在 Firefox 中工作

[英]Window.onblur function not working in Chrome, but works in Firefox

This works in Firefox, but not in Chrome:这适用于 Firefox,但不适用于 Chrome:

    window.onblur = () => {
      console.log('blur')
    }

I ran that code in Firefox and it worked, then ran the same code in Chrome, and it doesn't work.我在 Firefox 中运行了该代码,它运行正常,然后在 Chrome 中运行相同的代码,但它不起作用。

What am I missing?我错过了什么?

Edit:编辑:

Fiddle: https://jsfiddle.net/at6c0kL7小提琴: https://jsfiddle.net/at6c0kL7

Chrome: Version 88.0.4324.146 (Official Build) (x86_64) Chrome:版本 88.0.4324.146(官方构建)(x86_64)

Try using ES5 instead on ES6 syntax, like so:尝试在 ES6 语法上使用 ES5,如下所示:

 window.onblur = function(){ console.log('blur') }

Doesn't seem to be working for me either.似乎也不适合我。

You can try this instead:你可以试试这个:

document.addEventListener('visibilitychange', function(e) {
  if (document.visibilityState === 'hidden') {
    console.log('Hidden');
  }
});

Works with Chrome: Version 88.0.4324.150 (Official Build) (x86_64)适用于 Chrome:版本 88.0.4324.150(官方构建)(x86_64)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM