首頁  >  文章  >  web前端  >  JavaScript中Navigator物件的作用是什麼?

JavaScript中Navigator物件的作用是什麼?

王林
王林轉載
2023-09-04 23:45:071175瀏覽

JavaScript中Navigator物件的作用是什麼?

要獲取有關網頁目前運行的瀏覽器的信息,請使用內建導航器物件。您可以對該物件使用各種 Navigator 方法和屬性。

範例

您可以嘗試執行以下程式碼以在 JavaScript 中實作 Navigator 物件 -

<html>
   <head>
      <title>Navigator Object Example</title>
   </head>

   <body>
      <script>
         <!--
            var userAgent = navigator.userAgent;
            var opera = (userAgent.indexOf(&#39;Opera&#39;) != -1);
            var ie = (userAgent.indexOf(&#39;MSIE&#39;) != -1);
            var gecko = (userAgent.indexOf(&#39;Gecko&#39;) != -1);
            var netscape = (userAgent.indexOf(&#39;Mozilla&#39;) != -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>

以上是JavaScript中Navigator物件的作用是什麼?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除