我将哪些标题或javascript添加到HTML以拒绝旧版浏览器?

时间:2012-08-14 04:42:26

标签: javascript browser compatibility

开发前端实时javascript应用程序,我们决定放弃对旧浏览器的支持,因为它需要花费太多精力来支持它们。

我们应该在HTML中添加什么标题或javascript,以便当他们点击URL时,我可以将其重定向到页面以获取更新的浏览器,然后再继续使用我们的应用程序?

3 个答案:

答案 0 :(得分:2)

您可以使用导航器对象的以下属性

 navigator.appCodeName 
 navigator.appName 
 navigator.appVersion
 navigator.cookieEnabled 
navigator.platform 
 navigator.userAgent 

你也可以使用jquery浏览器对象 http://api.jquery.com/jQuery.browser/

答案 1 :(得分:1)

更好的选择是使用特征检测。例如,要测试浏览器是否具有地理位置支持,可以使用以下命令:

if (navigator.geolocation)
{
    // interact with geolocation features
}

<小时/> 但是,如果您坚持使用浏览器检测,则可以使用以下方法检测浏览器是否使用IE 8或更低版本:

function getInternetExplorerVersion()
// Returns the version of Windows Internet Explorer or a -1
// (indicating the use of another browser).
{
   var rv = -1; // Return value assumes failure.
   if (navigator.appName == 'Microsoft Internet Explorer')
   {
      var ua = navigator.userAgent;
      var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
      if (re.exec(ua) != null)
         rv = parseFloat( RegExp.$1 );
   }
   return rv;
}

var ver = getInternetExplorerVersion(); // example: 8.0
var belowIE8 = ver <= 8.0;

在此处找到:http://www.mkyong.com/javascript/how-to-detect-ie-version-using-javascript/

答案 2 :(得分:0)

如果您想根据特定的JavaScript版本确定这一点,请参阅下面的代码(未经测试):

<script language="javascript1.5">
var supported = true;
</script>
<script>
if ('undefined' === typeof supported) {
    alert('not at least 1.5 supported');
}

尽管如此,进行特征检测仍然会更好,因为这与你将要使用它的方式有更好的联系。