对于同一浏览器,在Javascript中使用不同的URL获取不同的版本信息

时间:2013-09-14 06:24:25

标签: javascript internet-explorer browser

您好我需要通过JavaScript检测浏览器版本。从here我能够得到我的预期。但是,当我通过localhost和主机名访问页面时,返回的版本不同。

使用以下源创建一个简单的HTML页面(BrowserVersion.html)。

<html>
<script>

var BrowserDetect = {
    init: function () {
    this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
    this.version = this.searchVersion(navigator.userAgent)
    || this.searchVersion(navigator.appVersion)
    || "an unknown version";
    this.OS = this.searchString(this.dataOS) || "an unknown OS";
    },
    searchString: function (data) {
    for (var i=0;i<data.length;i++) {
    var dataString = data[i].string;
    var dataProp = data[i].prop;
    this.versionSearchString = data[i].versionSearch || data[i].identity;
    if (dataString) {
    if (dataString.indexOf(data[i].subString) != -1)
    return data[i].identity;
    }
    else if (dataProp)
    return data[i].identity;
    }
    },
    searchVersion: function (dataString) {
    var index = dataString.indexOf(this.versionSearchString);
    if (index == -1) return;
    return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
    },
    dataBrowser: [
    {
    string: navigator.userAgent,
    subString: "Chrome",
    identity: "Chrome"
    },
    { string: navigator.userAgent,
    subString: "OmniWeb",
    versionSearch: "OmniWeb/",
    identity: "OmniWeb"
    },
    {
    string: navigator.vendor,
    subString: "Apple",
    identity: "Safari",
    versionSearch: "Version"
    },
    {
    prop: window.opera,
    identity: "Opera"
    },
    {
    string: navigator.vendor,
    subString: "iCab",
    identity: "iCab"
    },
    {
    string: navigator.vendor,
    subString: "KDE",
    identity: "Konqueror"
    },
    {
    string: navigator.userAgent,
    subString: "Firefox",
    identity: "Firefox"
    },
    {
    string: navigator.vendor,
    subString: "Camino",
    identity: "Camino"
    },
    { // for newer Netscapes (6+)
    string: navigator.userAgent,
    subString: "Netscape",
    identity: "Netscape"
    },
    {
    string: navigator.userAgent,
    subString: "MSIE",
    identity: "Explorer",
    versionSearch: "MSIE"
    },
    {
    string: navigator.userAgent,
    subString: "Gecko",
    identity: "Mozilla",
    versionSearch: "rv"
    },
    { // for older Netscapes (4-)
    string: navigator.userAgent,
    subString: "Mozilla",
    identity: "Netscape",
    versionSearch: "Mozilla"
    }
    ],
    dataOS : [
    {
    string: navigator.platform,
    subString: "Win",
    identity: "Windows"
    },
    {
    string: navigator.platform,
    subString: "Mac",
    identity: "Mac"
    },
    {
    string: navigator.userAgent,
    subString: "iPhone",
    identity: "iPhone/iPod"
    },
    {
    string: navigator.platform,
    subString: "Linux",
    identity: "Linux"
    }
    ]
    };
    BrowserDetect.init();
    </script>
<script type="text/javascript">
    navigator.sayswho= (function(){
  var N= navigator.appName, ua= navigator.userAgent, tem;
  var M= ua.match(/(opera|chrome|safari|firefox|msie)\/?\s*(\.?\d+(\.\d+)*)/i);
  if(M && (tem= ua.match(/version\/([\.\d]+)/i))!= null) M[2]= tem[1];
  M= M? [M[1], M[2]]: [N, navigator.appVersion,'-?'];
  return M;
 })();
 alert(navigator.sayswho);
 </script>

<html>

现在,当我使用以下网址访问上一页时,我得到的值为MSIE,8.0

http://localhost:8080/docs/BrowserVersion.html, 

但是当我使用以下URL访问同一页面时,我得到的值为MSIE,7.0。

http://mylocalhost:8080/docs/BrowserVersion.html

为什么浏览器版本在两个版本的URL之间发生变化。

注意:我的主机文件有以下条目

127.0.0.1   localhost
127.0.0.1   mylocalhost

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

您必须禁用IE的本地域兼容性视图。

转到IE的兼容性视图设置,取消选中第二个选项“在兼容性视图中显示Intranet站点”。


浏览器/浏览器版本的DOM对象检测的非常基础知识......

var z = document.getElementsByTagName('body')[0];

if (z.style.MozBinding!=undefined)
{
 browser='Firefox';
}
else if (window.opera)
{
//Opera up to 12.5
}
else if (z.style.scrollbar3dLightColor!=undefined)
{
 browser='msie';
}
else if (z.style.khtmlMarginBottomCollapse!=undefined && z.style.WebkitBorderRadius==undefined) {browser='Safari'; browser_version='2.0';}
else if (z.style.WebkitBorderRadius!=undefined || z.style.khtmlMarginBottomCollapse!=undefined)
{
 if (z.style.item=='function item() { [native code] }')
 {
  browser='Chrome';
 }
 else
 {
  browser='Safari';
 }
}
else if (z.style.KhtmlBorderRadius!=undefined) {browser='Konqueror';