最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Create Feature detection for One Javascript Feature (intersectionObserver) - Stack Overflow

programmeradmin3浏览0评论

Is there a way of storing a built-in javascript method in a variable to set different behaviour for when this method isn't available in certain browsers?

My specific case is for intersectionObserver which isn't available in Safari or older MS browsers. I have some animations triggered by this and would like to turn them off if intersectionObserver isn't available.

what I want to do essentially this:

var iO = intersectionObserver;

if ( !iO ) {
 // set other defaults
}

I don't really want to load a polyfill or library for just one feature?

Many thanks

Emily

Is there a way of storing a built-in javascript method in a variable to set different behaviour for when this method isn't available in certain browsers?

My specific case is for intersectionObserver which isn't available in Safari or older MS browsers. I have some animations triggered by this and would like to turn them off if intersectionObserver isn't available.

what I want to do essentially this:

var iO = intersectionObserver;

if ( !iO ) {
 // set other defaults
}

I don't really want to load a polyfill or library for just one feature?

Many thanks

Emily

Share Improve this question asked Feb 14, 2018 at 18:05 pjk_okpjk_ok 9679 gold badges52 silver badges108 bronze badges 5
  • 1 if("IntersectionObserver" in window)? var iO = window.IntersectionObserver;? – Sebastian Simon Commented Feb 14, 2018 at 19:00
  • That doesn't work - it also throws an error in my code editor @Xufox ? – pjk_ok Commented Feb 14, 2018 at 19:38
  • What does? Which error? – Sebastian Simon Commented Feb 14, 2018 at 19:47
  • That line of code. I tried using that to create the variable, but it underlines it as an error in my editor and won't process the code? Also i've never seen an if statement bined with two question marks before. Am i supposed to include the question marks? Unless that's shorthand for something I'm meant to know? I'm really confused. – pjk_ok Commented Feb 15, 2018 at 2:49
  • I haven't marked up the question marks as code, so they do not belong in the code. – Sebastian Simon Commented Feb 15, 2018 at 11:28
Add a ment  | 

1 Answer 1

Reset to default 18 +50

The in Operator is widely used to detect features supported by the browser. JavaScript features are globally available as a property to window object. So we can check if window element has the property.

if("IntersectionObserver" in window){
    /* work with IntersectionObserver */
}
if("FileReader" in window){
    /* work with FileReader */
}

The in operator returns true if the specified property is in the specified object or its prototype chain.
Syntax: prop in object
[source: developer.mozilla]

So you can also save the result in a boolean variable and use it later in your code.

var iO = "IntersectionObserver" in window; /* true if supported */

if ( !iO ) {
 // set other defaults
}
发布评论

评论列表(0)

  1. 暂无评论