Javascript – Browsers

Điều quan trọng là phải hiểu sự khác biệt giữa các trình duyệt khác nhau để xử lý từng trình duyệt theo cách được mong đợi. Vì vậy, điều quan trọng là phải biết trang web của bạn đang chạy trên trình duyệt nào.

Để nhận thông tin về trình duyệt mà trang web của bạn hiện đang chạy, hãy sử dụng đối tượng điều hướng tích hợp sẵn.

Navigator Properties

Có một số thuộc tính liên quan đến Bộ điều hướng mà bạn có thể sử dụng trong trang Web của mình. Sau đây là danh sách tên và mô tả của từng loại.

Sr.No.Property & Description
1appCodeName This property is a string that contains the code name of the browser, Netscape for Netscape and Microsoft Internet Explorer for Internet Explorer.
2appVersion This property is a string that contains the version of the browser as well as other useful information such as its language and compatibility.
3language This property contains the two-letter abbreviation for the language that is used by the browser. Netscape only.
4mimTypes[] This property is an array that contains all MIME types supported by the client. Netscape only.
5platform[] This property is a string that contains the platform for which the browser was compiled.”Win32″ for 32-bit Windows operating systems
6plugins[] This property is an array containing all the plug-ins that have been installed on the client. Netscape only.
7userAgent[] This property is a string that contains the code name and version of the browser. This value is sent to the originating server to identify the client.

Navigator Methods

Sr.No.Description
1javaEnabled() This method determines if JavaScript is enabled in the client. If JavaScript is enabled, this method returns true; otherwise, it returns false.
2plugings.refresh This method makes newly installed plug-ins available and populates the plugins array with all new plug-in names. Netscape only.
3preference(name,value) This method allows a signed script to get and set some Netscape preferences. If the second parameter is omitted, this method will return the value of the specified preference; otherwise, it sets the value. Netscape only.
4taintEnabled() This method returns true if data tainting is enabled; false otherwise.

Browser Detection

Có một JavaScript đơn giản có thể được sử dụng để tìm ra tên của trình duyệt và sau đó, một trang HTML có thể được cung cấp cho người dùng.

<html>   
   <head>
      <title>Browser Detection Example</title>
   </head>
   
   <body>      
      <script type = "text/javascript">
         <!--
            var userAgent   = navigator.userAgent;
            var opera       = (userAgent.indexOf('Opera') != -1);
            var ie          = (userAgent.indexOf('MSIE') != -1);
            var gecko       = (userAgent.indexOf('Gecko') != -1);
            var netscape    = (userAgent.indexOf('Mozilla') != -1);
            var version     = navigator.appVersion;
            
            if (opera) {
               document.write("Opera based browser");
               // Keep your opera specific URL here.
            } else if (gecko) {
               document.write("Mozilla based browser");
               // Keep your gecko specific URL here.
            } else if (ie) {
               document.write("IE based browser");
               // Keep your IE specific URL here.
            } else if (netscape) {
               document.write("Netscape based browser");
               // Keep your Netscape specific URL here.
            } else {
               document.write("Unknown browser");
            }
            
            // You can include version to along with any above condition.
            document.write("<br /> Browser version info : " + version );
         //-->
      </script>      
   </body>
</html>