標題: Javascript - if IE偵測 - (全版本IE) Base on User-Agent [打印本頁] 作者: 死神 時間: 2015-7-24 14:31 標題: Javascript - if IE偵測 - (全版本IE) Base on User-Agent 恨透了IE嗎? 以下我整理了偵測IE的Javascript,目前最高支援到IE12,原理就是判斷user-agent。
// detect IE
var IEversion = detectIE();
if (IEversion) {
alert('IE version: '+IEversion);
} else {
alert('Not IE browser');
};
/**
* detect IE
* returns version of IE or false, if browser is not Internet Explorer
*/
function detectIE() {
var ua = window.navigator.userAgent;
// test values
// IE 10
//ua = 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)';
// IE 11
//ua = 'Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko';
// IE 12 / Spartan
//ua = 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36 Edge/12.0';