简体   繁体   中英

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

This works in Firefox, but not in 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.

What am I missing?

Edit:

Fiddle: https://jsfiddle.net/at6c0kL7

Chrome: Version 88.0.4324.146 (Official Build) (x86_64)

Try using ES5 instead on ES6 syntax, like so:

 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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