What is the correct way to detect Opera using jQuery?

前端 未结 9 1725
梦毁少年i
梦毁少年i 2021-02-19 04:52

Amazon.com recently updated their javascript, and it\'s causing problems with some Opera browsers.

Their browser detection code looks like so, but it\'s faulty:

相关标签:
9条回答
  • 2021-02-19 05:33

    It is much better to detect javascript capabilities rather than browser userAgent.

    ie DOM, XmlHttpRequest, eventing model (event.target vs event.srcElement), ActiveX, Java etc

    By focusing on the API functions that you will require, rather than a target browser you will create a more robust set of scripts, and inevitably less special casing.

    This link here at opera will probably tell you more

    0 讨论(0)
  • 2021-02-19 05:35

    In current HTML5 times, you can also check for browser features instead often.

    if (!window.FormData) { alert("xmlhttprequest L2 FormData interface not available"); }
    
    0 讨论(0)
  • 2021-02-19 05:36

    I think this way is the best
    if ( window.opera.version() == 12) { }
    This example check if opera version is 12. Very useful when I have problems with font-face in Opera.

    0 讨论(0)
  • 2021-02-19 05:40

    I check for Opera like this:

    if (/Opera/.test (navigator.userAgent)) // do something
    

    Why would you want jQuery?

    0 讨论(0)
  • 2021-02-19 05:49

    Prior to jQuery 1.3, you could use jQuery.browser:

    if( $.browser.opera ){
      alert( "You're using Opera version "+$.browser.version+"!" );
    }
    

    From version 1.3, you should use jQuery.support instead.

    Main reason for this is that should should avoid checking for browsers, as features may change from version to version, making your code obsolete in no time.

    You should always try to use feature detection instead. This will allow you to see if current browser supports the feature you're trying to use, regardless the browser brand, version, etc.

    0 讨论(0)
  • 2021-02-19 05:50

    There is a special window.opera object which is present in all Opera 5+ browsers. So something as simple as:

    if (window.opera && window.opera.buildNumber) { 
        // we are in Opera 
    }
    

    would be enough.

    0 讨论(0)
提交回复
热议问题