NavigatorLanguage.language

NavigatorLanguage.language 只读属性返回一个表示用户偏好语言的字符串,通常指浏览器 UI 的语言。

语法

let lang = navigator.language;

一个 DOMStringlang 存储一个表示语言版本(在 BCP 47 中定义)的字符串。合法的语言版本有 "zh-CN"、"en"、"en-US"、"fr"、"es-ES" 等。

注意 macOS 和 iOS 平台上的 Safari(10.2 之前版本),国家代码为小写:"zh-cn"、"en-us"、"fr-fr" 等。

示例

if ( window.navigator.language != 'zh-CN' ) {       
  doLangSelect(window.navigator.language); 
}

标准

标准 状态 备注
HTML Living Standard
NavigatorLanguage: language
Living Standard 初次定义

浏览器兼容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
languageChrome Full support 1
Notes
Full support 1
Notes
Notes Returns the browser UI language, not the value of the Accept-Language HTTP header.
Edge Full support 12
Notes
Full support 12
Notes
Notes In Chromium versions of Edge, this returns the browser UI language, not the value of the Accept-Language HTTP header.
Firefox Full support 1
Notes
Full support 1
Notes
Notes Prior to Firefox 4, this property's value was also part of the user agent string, as reported by navigator.userAgent. Starting in Firefox 5, this property's value is based on the value of the Accept-Language HTTP header.
IE Full support 11
Notes
Full support 11
Notes
Notes Closest available (non-standard) properties are userLanguage and browserLanguage.
Opera Full support 4Safari Full support 1WebView Android Full support 1
Notes
Full support 1
Notes
Notes Returns the browser UI language, not the value of the Accept-Language HTTP header.
Chrome Android Full support 18
Notes
Full support 18
Notes
Notes Returns the browser UI language, not the value of the Accept-Language HTTP header.
Firefox Android Full support 4Opera Android Full support 10.1Safari iOS Full support 1Samsung Internet Android Full support 1.0
Notes
Full support 1.0
Notes
Notes Returns the browser UI language, not the value of the Accept-Language HTTP header.

Legend

Full support  
Full support
See implementation notes.
See implementation notes.

另请参阅