userAgent property
[This documentation is preliminary and is subject to change.]
Retrieves a string equivalent to the HTTP user-agent request header.
Syntax
| JavaScript | |
|---|
Property values
Type: String
String that specifies a valid HTTP user agent.
Remarks
The user-agent request header contains information about compatibility, the client, and the platform name. For more information about retrieving the application name, see the appName property. For more information about the platform and version, see the appVersion property.
The userAgent property dynamically returns a different value depending on the browser and platform versions.
For example, Microsoft Internet Explorer 6 returns the following string for Windows XP.
Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)
For example, the following string shows compatibility with WWAHost.exe 1.0.
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0; WWAHost/1.0;)
Examples
This example uses the userAgent property to specify a required platform before additional script is executed. For example, if Windows XP is a requirement for the document, you can use a variable to determine whether the user is running the necessary operating system. The "bIsXP" variable is set to true if Windows XP is found in the userAgent value and the additional script is processed.
<SCRIPT>
var bIsXP=false;
window.onload=fnInit;
function fnInit(){
if(navigator.userAgent.indexOf("Windows NT 5.1")>-1){
bIsXP=true;
}
if(bIsXP==true){
// Process additional script.
}
}
</SCRIPT>
The following function retrieves the user agent string and returns it through the out parameter, pbstrUA.
// Equivalent to document.parentWindow.navigator.userAgent
HRESULT GetUA(IHTMLDocument2* pDoc, BSTR* pbstrUA)
{
IHTMLWindow2* pWindow = NULL;
if (!pDoc)
{
return E_INVALIDARG;
}
hr = pDoc->get_parentWindow(&pWindow);
if (FAILED(hr) || !pWindow)
{
return hr;
}
IOmNavigator* pNavigator = NULL;
hr = pWindow->get_navigator(&pNavigator);
if (FAILED(hr) || !pNavigator)
{
goto Error;
}
hr = pNavigator->get_userAgent(pbstrUA);
Error:
if (pWindow) pWindow->Release();
if (pNavigator) pNavigator->Release();
return hr;
}
See also
- clientInformation
- navigator
- Navigator Constructor
- About Conditional Comments
Build date: 2/14/2012
- 1/13/2009
- jpsanders