それはあなたが本当にやりたいことですが、ブラウザの検出を回避し、機能を確認するのが最善の方法です。Canvas、Audio、WebSocketsなど、単純な JavaScript 呼び出しまたは CSS を介して、私にとって最善の方法はModernizRのようなツールを使用することです。
navigator.userAgent
(ユーザーが構成可能な)プロパティによってブラウザーを検出する「UA スニッフィング」を実行する従来の (しかし非常に信頼性の低い) 方法とは異なり、 Modernizr は実際の機能検出を行って、さまざまなブラウザーができることとできないことを確実に識別します。
CSS を使用している場合は、次のようにできます。
.no-js .glossy,
.no-cssgradients .glossy {
background: url("images/glossybutton.png");
}
.cssgradients .glossy {
background-image: linear-gradient(top, #555, #333);
}
要素のクラス名としてすべての機能をロードして追加し<html>
、CSS に関して必要に応じて行うことができるためです。
また、機能にファイルをロードすることもできます。たとえば、ブラウザがネイティブ サポートされていない場合は、ポリフィル js と css をロードします。
Modernizr.load([
// Presentational polyfills
{
// Logical list of things we would normally need
test : Modernizr.fontface && Modernizr.canvas && Modernizr.cssgradients,
// Modernizr.load loads css and javascript by default
nope : ['presentational-polyfill.js', 'presentational.css']
},
// Functional polyfills
{
// This just has to be truthy
test : Modernizr.websockets && window.JSON,
// socket-io.js and json2.js
nope : 'functional-polyfills.js',
// You can also give arrays of resources to load.
both : [ 'app.js', 'extra.js' ],
complete : function () {
// Run this after everything in this group has downloaded
// and executed, as well everything in all previous groups
myApp.init();
}
},
// Run your analytics after you've already kicked off all the rest
// of your app.
'post-analytics.js'
]);
JavaScript から機能をリクエストする簡単な例:
http://jsbin.com/udoyic/1