Click handler on <video> conflicts with Firefox's native behaviour

后端 未结 2 991
北荒
北荒 2021-01-19 17:00

I\'ve added a video to my site using the default HTML5 video player. The code is like this:

2条回答
  •  北海茫月
    2021-01-19 17:49

    There may very likely be better ways to handle this, but here's one that I've come up with: I looked through the release notes for Firefox 35, and it looks like one of the changes made in 35 was fixing a bug where a method called .hasAttributes() that, according to spec, is supposed to be located on Element was previously located on Node. So, although it looks odd, you could do something like:

    if(typeof InstallTrigger !== 'undefined' && 
       typeof Element.prototype.hasAttributes !== 'undefined') {
        // is Firefox >= 35
    } 
    

    This is based on the fact that typeof InstallTrigger !== 'undefined' would identify Firefox as per this answer and we know .hasAttributes moved to Element beginning in version 35. This would be preferable to user agent parsing because unlike the User Agent string it is unlikely to be spoofed in any way.

    It's been mentioned in the comments that it seems odd to do a form of browser detection by checking for the presence of an unrelated JavaScript object - but this is a practice that's established and has been used historically to detect versions of a specific browser greater than a certain version: Here's an article that describes commonly used variables that can be used to detect Internet Explorer versions >= a given number.

提交回复
热议问题