轉載請注明原文地址:https://www.cnblogs.com/litou/p/10772272.htm
在客戶端檢查用戶使用的瀏覽器類型和版本,都是根據navigator.userAgent屬性去判斷的,雖然說一些自定義的瀏覽器或者HTTP組件會修改這個屬性,但對於一般的情況下是足夠的了。一般情況下userAgent值如下:
Chrome:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36
FireFox:Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:66.0) Gecko/20100101 Firefox/66.0
IE11:Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727; .NET CLR 3.0.30729; .NET CLR 3.5.30729; LCTE; rv:11.0) like Gecko
IE8:Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)
有一種特殊的情況,就是使用WebBrowser控件瀏覽網頁,WebBrowser控件是微軟提供的對IE內核Trident二次開發的封裝,在WebBrowser控件中檢測到的userAgent值如下:
IE11下的WebBrowser:Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.2; WOW64; Trident/7.0; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727; .NET CLR 3.0.30729; .NET CLR 3.5.30729; LCTE)
IE8下的WebBrowser:Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)
可以發現不管操作系統中是哪個IE版本,userAgent值中都指示IE版本為MSIE 7.0,這會影響檢測瀏覽器版本的正確性。
這個原因是在於WebBrowser控件默認是與運行在IE7的瀏覽器模式下(https://blogs.msdn.microsoft.com/patricka/2015/01/12/controlling-webbrowser-control-compatibility),如果要修改WebBrowser控件的瀏覽器模式,需要修改注冊表才能實現(https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/general-info/ee330730(v%3dvs.85))。又或者在HTML的head中指定X-UA-Compatible這個meta,這樣WebBrowser控件會強行運行在指定的文檔模式中。
這里提到IE的兩個概念,瀏覽器模式和文檔模式。
瀏覽器模式:切換IE針對網頁的默認文檔模式、對不同版本瀏覽器的條件備注解析、發送給網站服務器的用戶代理(User-Agent)字符串的值。(圖1)
文檔模式:指定IE的頁面排版引擎(Trident)以哪個版本的方式來解析並渲染網頁代碼。(圖2)
簡單來說,文檔模式決定渲染網頁時內核引擎的版本,而瀏覽器模式影響的是文檔模】、8式的默認版本,以及userAgent串的值,所以我們獲取到的userAgent串都是瀏覽器或者WebBrowser控件的瀏覽器模式值,也就是說,對於一般的瀏覽器內核、版本,以及操作系統的檢測,通過userAgent串是可以區分出來的,但對於WebBrowser控件中的檢測不適用。
是不是就沒有辦法呢?這個是否定的,但可能需要繞個圈。經測試,IE瀏覽器默認情況下的瀏覽器模式和文檔模式值對應如下:
瀏覽器模式 | 文檔模式 | |||
可選值 | 默認值 | 可選值 | 默認值 | |
IE8 | 8、7 | 8 | 8、7、Quirks | 8 |
IE9 | 9、8、7 | 9 | 9、8、7、Quirks | 9 |
IE10 | 10、9、8、7 | 10 | 標准、9、8、7、5Quirks、Quirks | 標准 |
IE11 | 11、10、9、8、7、6 | 11 | 11、10、9、8、7、5 | 11 |
由於WebBrowser控件的瀏覽器模式修改修改注冊表才能實現修改,這個估計一般很難做到,但是上面提到了,在HTML的head中指定X-UA-Compatible這個meta,這樣WebBrowser控件會強行運行在指定的文檔模式中,也就是說,我們可以指定X-UA-Compatible的值,然后在文檔中通過document.documentMode來讀取當前文檔模式的值,就可以達到目的。操作步驟如下:
1)HTML頁面的head中,在title和所有的meta前,增加meta為<meta http-equiv="X-UA-Compatible" content="IE=edge" />,這樣就可以使文檔模式運行在最高版本。(文檔模式的最高版本與瀏覽器版本一致)
2)通過document.documentMode屬性讀取文檔模式的值,進而判斷出當前IE瀏覽器的版本
通過測試整理,匯總結果如下:
userAgent | 設置meta后的documentMode值 | |||
原生瀏覽器 | WebBrowser控件 | 原生瀏覽器 | WebBrowser控件 | |
IE8 / XP | Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0) | Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0) | 8 | 8 |
IE8 / Win7 | Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0) | Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/4.0) | 8 | 8 |
IE9 / Win7 x64 | Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0) | Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0) | 9 | 9 |
IE10 / Win7 x64 | Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0) | Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/6.0) | 10 | 10 |
IE11 / Win7 x64 | Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0) like Gecko | Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/7.0) | 11 | 11 |
IE11 / Win10 x64 | Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0) like Gecko | Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.2; WOW64; Trident/7.0) | 11 | 11 |
其實最后發現,userAgent中根據Trident后面的版本號也可以很好的判斷出IE瀏覽器的版本:)