In the process of developing a Web project, it is sometimes necessary to detect whether the client has installed certain necessary software to ensure the normal operation of the project. This article will introduce how to use JavaScript to determine whether a certain software is installed on the client.
Before starting to judge, you first need to obtain client-related information, including browser type and version number. Generally, this information can be obtained through the navigator object. The code is as follows:
var sUserAgent = navigator.userAgent.toLowerCase(); var bIsIE = sUserAgent.match(/msie/i) == "msie"; var bIsFirefox = sUserAgent.match(/firefox/i) == "firefox"; var bIsChrome = sUserAgent.match(/chrome/i) == "chrome";
This code first obtains the client's userAgent string and converts it into lowercase letters. Then use regular expressions to match whether it is IE, Firefox and Chrome browsers respectively. If the match is successful, return true, otherwise return false.
After obtaining the client-related information, you can judge based on the characteristics of different browsers. The following is how common browsers determine it.
The IE browser itself has some ActiveX controls that can be used to determine whether certain software is installed. The following code demonstrates how to use IE's ActiveXObject to determine whether the client has Adobe Reader installed:
try { var adobeReader = new ActiveXObject('AcroPDF.PDF'); if (adobeReader) { alert('已安装Adobe Reader!'); } } catch (e) { alert('未安装Adobe Reader!'); }
In the IE browser, a JavaScript object can be created through ActiveXObject, which can call its own methods and properties. In the above code, the AcroPDF.PDF object is used to determine whether Adobe Reader is installed. If the object is created successfully, the software has been installed, otherwise it has not been installed.
Firefox browser does not have ActiveXObject, so other methods need to be used to determine whether a certain software is installed. The following code demonstrates how to use Firefox's MIME type to determine whether Adobe Reader is installed:
var mimeType = navigator.mimeTypes['application/pdf']; if (mimeType && mimeType.enabledPlugin) { alert('已安装Adobe Reader!'); } else { alert('未安装Adobe Reader!'); }
In the Firefox browser, you can obtain the MIME type through navigator.mimeTypes, and then determine whether it is enabled through the enabledPlugin attribute of the MIME type. Plugin installed. In the above code, the MIME type of application/pdf is first obtained, and then its enabledPlugin attribute is determined. If the value is true, it means that Adobe Reader has been installed, otherwise it has not been installed.
Chrome browser is similar to Firefox and can also use MIME types to determine whether a certain software is installed. The following code demonstrates how to use Chrome's MIME type to determine whether Adobe Reader is installed:
var plugins = navigator.plugins; var mimeTypes = plugins['application/pdf'] || plugins['application/x-pdf']; if (mimeTypes) { alert('已安装Adobe Reader!'); } else { alert('未安装Adobe Reader!'); }
In the Chrome browser, you can obtain the plug-in list through navigator.plugins, and then determine whether it is installed based on different MIME types. Got a certain software. In the above code, plugins['application/pdf'] and plugins['application/x-pdf'] represent the MIME types of application/pdf and application/x-pdf respectively. If any of them is not empty, it means Adobe Reader is already installed, otherwise it is not installed.
This article introduces how to use JavaScript to determine whether a certain software is installed on the client. By obtaining client-related information and making judgments based on different browser features, the purpose of detecting whether the software is installed can be achieved. In actual projects, relevant codes can also be adjusted as needed to achieve better results.
The above is the detailed content of How to use javascript to determine whether a certain software is installed on the client. For more information, please follow other related articles on the PHP Chinese website!