首页 >web前端 >html教程 >JavaScript中Navigator对象的作用是什么?

JavaScript中Navigator对象的作用是什么?

王林
王林转载
2023-09-04 23:45:071258浏览

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删除