2013-11-26 84 views
-8

以下JavaScript代碼在幾個月前工作,但現在不再了。有任何想法嗎?Opera瀏覽器檢測不再工作

If (window.opera) { 
alert ("The Opera browser is not supported on this website.") 
} 
+5

檢測功能而不是瀏覽器。 – OneOfOne

+0

是的,我正在檢查功能,但想要更具體。如果(document.hasFocus)setInterval(「myTimer()」,1000); \t else alert(「您的瀏覽器不支持hasFocus方法,此方法在本網站中是必需的,您必須使用不同的瀏覽器。」);} – Medeno

+0

@Medeno您定位的是哪些瀏覽器?爲什麼? – karlcow

回答

-2

使用此:

var nVer = navigator.appVersion; 
var nAgt = navigator.userAgent; 
var browserName = navigator.appName; 
var fullVersion = ''+parseFloat(navigator.appVersion); 
var majorVersion = parseInt(navigator.appVersion,10); 
var nameOffset,verOffset,ix; 

// In Opera, the true version is after "Opera" or after "Version" 
if ((verOffset=nAgt.indexOf("Opera"))!=-1) { 
browserName = "Opera"; 
fullVersion = nAgt.substring(verOffset+6); 
if ((verOffset=nAgt.indexOf("Version"))!=-1) 
    fullVersion = nAgt.substring(verOffset+8); 
} 
// In MSIE, the true version is after "MSIE" in userAgent 
else if ((verOffset=nAgt.indexOf("MSIE"))!=-1) { 
browserName = "Microsoft Internet Explorer"; 
fullVersion = nAgt.substring(verOffset+5); 
} 
// In Chrome, the true version is after "Chrome" 
else if ((verOffset=nAgt.indexOf("Chrome"))!=-1) { 
browserName = "Chrome"; 
fullVersion = nAgt.substring(verOffset+7); 
} 
// In Safari, the true version is after "Safari" or after "Version" 
else if ((verOffset=nAgt.indexOf("Safari"))!=-1) { 
browserName = "Safari"; 
fullVersion = nAgt.substring(verOffset+7); 
if ((verOffset=nAgt.indexOf("Version"))!=-1) 
    fullVersion = nAgt.substring(verOffset+8); 
} 
// In Firefox, the true version is after "Firefox" 
else if ((verOffset=nAgt.indexOf("Firefox"))!=-1) { 
browserName = "Firefox"; 
fullVersion = nAgt.substring(verOffset+8); 
} 
// In most other browsers, "name/version" is at the end of userAgent 
else if ((nameOffset=nAgt.lastIndexOf(' ')+1) < 
      (verOffset=nAgt.lastIndexOf('/'))) 
{ 
browserName = nAgt.substring(nameOffset,verOffset); 
fullVersion = nAgt.substring(verOffset+1); 
if (browserName.toLowerCase()==browserName.toUpperCase()) { 
    browserName = navigator.appName; 
} 
} 
// trim the fullVersion string at semicolon/space if present 
if ((ix=fullVersion.indexOf(";"))!=-1) 
    fullVersion=fullVersion.substring(0,ix); 
if ((ix=fullVersion.indexOf(" "))!=-1) 
    fullVersion=fullVersion.substring(0,ix); 

majorVersion = parseInt(''+fullVersion,10); 
if (isNaN(majorVersion)) { 
fullVersion = ''+parseFloat(navigator.appVersion); 
majorVersion = parseInt(navigator.appVersion,10); 
} 

document.write('' 
+'Browser name = '+browserName+'<br>' 
+'Full version = '+fullVersion+'<br>' 
+'Major version = '+majorVersion+'<br>' 
+'navigator.appName = '+navigator.appName+'<br>' 
+'navigator.userAgent = '+navigator.userAgent+'<br>' 
) 

和U會得到這樣的輸出:

Browser name = Google Chrome 
Full version = 31.0.1650.57 
Major version = 31 
navigator.appName = Netscape 
navigator.userAgent = Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36 
3

它看起來像這個問題是固定的https://code.google.com/p/chromium/issues/detail?id=64846

(在未來,請測試功能不會嗅探useragent字符串。)

+0

是的,我正在檢查功能,請參閱對原始問題的回覆。我覺得最好讓用戶知道「歌劇不被支持」,而不是「hasFocus功能不被支持」,但正如你所說,hasFocus現在正在Opera上工作。我會檢查功能。謝謝 – Medeno

+0

hasFocus在Opera中無法再工作。 – Medeno