<p>I wanted some of those spiffy rounded corners for a web project that I'm currently working on.</p>

<p>I thought I'd try to accomplish it using javascript and not CSS in an effort to keep the requests for image files to a minimum (yes, I know that it's possible to combine all required rounded corner shapes into one image) and I also wanted to be able to change the background color pretty much on the fly.</p>

<p>I already utilize jQuery so I looked at the excellent <a href="http://plugins.jquery.com/project/corners" rel="nofollow noreferrer">rounded corners plugin</a> and it worked like a charm in every browser I tried. Being a developer however I noticed the opportunity to make it a bit more efficient. The script already includes code for detecting if the current browser supports webkit rounded corners (safari based browsers). If so it uses raw CSS instead of creating layers of divs.</p>

<p>I thought that it would be awesome if the same kind of check could be performed to see if the browser supports the Gecko-specific <code>-moz-border-radius-*</code> properties and if so utilize them.</p>

<p>The check for webkit support looks like this:</p>

<pre><code>var webkitAvailable = false;  
try {  
    webkitAvailable = (document.defaultView.getComputedStyle(this[0], null)['-webkit-border-radius'] != undefined);
} 
catch(err) {}
</code></pre>

<p>That, however, did not work for <code>-moz-border-radius</code> so I started checking for alternatives.</p>

<p>My fallback solution is of course to use browser detection but that's far from recommended practice ofcourse.</p>

<p>My best solution yet is as follows.</p>

<pre><code>var mozborderAvailable = false;
try {
    var o = jQuery('&lt;div&gt;').css('-moz-border-radius', '1px');
    mozborderAvailable = $(o).css('-moz-border-radius-topleft') == '1px';
    o = null;
} catch(err) {}
</code></pre>

<p>It's based on the theory that Gecko "expands" the composite -moz-border-radius to the four sub-properties</p>

<ul>
<li><code>-moz-border-radius-topleft</code></li>
<li><code>-moz-border-radius-topright</code></li>
<li><code>-moz-border-radius-bottomleft</code></li>
<li><code>-moz-border-radius-bottomright</code></li>
</ul>

<p>Is there any javascript/CSS guru out there that have a better solution?</p>

<p>(The feature request for this page is at <a href="http://plugins.jquery.com/node/3619" rel="nofollow noreferrer">http://plugins.jquery.com/node/3619</a>)</p>

[{'answer_id': 19080, 'author': 'M. Dave Auayan', 'author_id': 2007, 'author_profile': 'https://Stackoverflow.com/users/2007', 'pm_score': 2, 'selected': False, 'text': "<p>Why not use <code>-moz-border-radius</code> and <code>-webkit-border-radius</code> in the stylesheet? It's valid CSS and throwing an otherwise unused attribute would hurt less than having javascript do the legwork of figuring out if it should apply it or not. </p>\n\n<p>Then, in the javascript you'd just check if the browser is IE (or Opera?) - if it is, it'll ignore the proprietary tags, and your javascript could do it's thing.</p>\n\n<p>Maybe I'm missing something here...</p>\n"}, {'answer_id': 19203, 'author': 'Nickolay', 'author_id': 1026, 'author_profile': 'https://Stackoverflow.com/users/1026', 'pm_score': 1, 'selected': False, 'text': '<p>Apply CSS unconditionally and check <code>element.style.MozBorderRadius</code> in the script?</p>\n'}, {'answer_id': 19329, 'author': 'travis', 'author_id': 1414, 'author_profile': 'https://Stackoverflow.com/users/1414', 'pm_score': 5, 'selected': True, 'text': '<p>How about this?</p>\n\n<pre><code>var mozborderAvailable = false;\ntry {\n  if (typeof(document.body.style.MozBorderRadius) !== "undefined") {\n    mozborderAvailable = true;\n  }\n} catch(err) {}\n</code></pre>\n\n<p>I tested it in Firefox 3 (true) and false in: Safari, IE7, and Opera.</p>\n\n<p>(Edit: better undefined test)</p>\n'}, {'answer_id': 19506, 'author': 'Ian Oxley', 'author_id': 1904, 'author_profile': 'https://Stackoverflow.com/users/1904', 'pm_score': 1, 'selected': False, 'text': '<p>As you\'re already using jQuery you could use <a href="http://docs.jquery.com/Utilities/jQuery.browser" rel="nofollow noreferrer">jQuery.browser</a> utility to do some browser sniffing and then target your CSS / JavaScript accordingly.</p>\n'}, {'answer_id': 222751, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': '<p>The problem with this is that Firefox 2 does not use anti-aliasing for the borders. The script would need to detect for Firefox 3 before is uses native rounded corners as FF3 does use anti-aliasing.</p>\n'}, {'answer_id': 1996390, 'author': 'Cybolic', 'author_id': 242846, 'author_profile': 'https://Stackoverflow.com/users/242846', 'pm_score': 1, 'selected': False, 'text': "<p>I've developed the following method for detecting whether the browser supports rounded borders or not. I have yet to test it on IE (am on a Linux machine), but it works correctly in Webkit and Gecko browsers (i.e. Safari/Chrome and Firefox) as well as in Opera:</p>\n\n<pre><code>function checkBorders() {\n  var div = document.createElement('div');\n  div.setAttribute('style', '-moz-border-radius: 8px; -webkit-border-radius: 8px; border-radius: 8px;');\n  for ( stylenr=0; stylenr&lt;div.style.length; stylenr++ ) {\n    if ( /border.*?-radius/i.test(div.style[stylenr]) ) {\n      return true;\n    };\n  return false;\n};\n</code></pre>\n\n<p>If you wanted to test for Firefox 2 or 3, you should check for the Gecko rendering engine, not the actual browser. I can't find the precise release date for Gecko 1.9 (which is the version that supports anti-aliased rounded corners), but the Mozilla wiki says it was released in the first quarter of 2007, so we'll assume May just to be sure.</p>\n\n<pre><code>if ( /Gecko\\/\\d*/.test(navigator.userAgent) &amp;&amp; parseInt(navigator.userAgent.match(/Gecko\\/\\d*/)[0].split('/')[1]) &gt; 20070501 )\n</code></pre>\n\n<p>All in all, the combined function is this:</p>\n\n<pre><code>function checkBorders() {\n  if ( /Gecko\\/\\d*/.test(navigator.userAgent) &amp;&amp; parseInt(navigator.userAgent.match(/Gecko\\/\\d*/)[0].split('/')[1]) &gt; 20070501 ) {\n    return true;\n  } else {\n    var div = document.createElement('div');\n    div.setAttribute('style', '-moz-border-radius: 8px; -webkit-border-radius: 8px; border-radius: 8px;');\n    for ( stylenr=0; stylenr&lt;div.style.length; stylenr++ ) {\n      if ( /border.*?-radius/i.test(div.style[stylenr]) ) {\n        return true;\n      };\n    return false;\n  };\n};\n</code></pre>\n"}, {'answer_id': 3458857, 'author': 'vernonk', 'author_id': 223910, 'author_profile': 'https://Stackoverflow.com/users/223910', 'pm_score': 2, 'selected': False, 'text': '<p>I know this is an older question, but it shows up high in searches for testing border-radius support so I thought I\'d throw this nugget in here.</p>\n\n<p>Rob Glazebrook has a little snippet that extends the support object of jQuery to do a nice quick check for border-radius support (also moz and web-kit).</p>\n\n<pre><code>jQuery(function() {\njQuery.support.borderRadius = false;\njQuery.each([\'BorderRadius\',\'MozBorderRadius\',\'WebkitBorderRadius\',\'OBorderRadius\',\'KhtmlBorderRadius\'], function() {\n    if(document.body.style[this] !== undefined) jQuery.support.borderRadius = true;\n    return (!jQuery.support.borderRadius);\n}); });\n</code></pre>\n\n<p><a href="http://www.cssnewbie.com/test-for-border-radius-support/" rel="nofollow noreferrer">Attribution</a></p>\n\n<p>That way, if there isn\'t support for it you can fall back and use jQuery to implement a 2-way slider so that other browsers still have a similar visual experience.</p>\n'}]
['https://Stackoverflow.com/questions/18077', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2114/']