如何测试浏览器是否支持loading='lazy'?

时间:2021-03-13 09:44:02

标签: javascript

浏览器级别的延迟加载是几乎所有浏览器的新功能 (https://developer.mozilla.org/en-US/docs/Web/Performance/Lazy_loading)。

我想测试浏览器是否支持它,如果不支持,则为其添加 polyfill(https://github.com/mfranzke/loading-attribute-polyfill#:~:text=loading%3D%22lazy%22%20attribute%20polyfill,not%20just%20thatfor%20%2D%20SEO%20friendly.)。

我怎样才能实现它?谢谢。

1 个答案:

答案 0 :(得分:2)

Google recommends

if ('loading' in HTMLImageElement.prototype) {
  // supported in browser
} else {
  // fetch polyfill/third-party library
}
相关问题