用js判断不同浏览器

 

简述:<script>(function () { // 检测浏览器类型 function detectBrowser() { const userAgent = navigator.userAgent; // 检查 Microsoft Edge(包含 &#39;Edg&#39; 且排除 Safari) if (userAgent.indexOf(&#39;Edg&#39;) > -1 && userAgent.indexOf(&#39;Chrome&#39;) > -1 && userAgent.indexOf(&#39;Safari&#39;) > -1) { alert(&#39;Microsoft Edge&#39;); return &#39;Microsoft Edge&#39;; } // 检查 Chrome(只包含 &#39;Chrome&#39; 且没有 &#39;Edg&#39;) if (userAgent.indexOf(&#39;Chrome&#39;) > -1 && userAgent.in...

详情:


<script>

(function () {

  // 检测浏览器类型

  function detectBrowser() {

    const userAgent = navigator.userAgent;


    // 检查 Microsoft Edge(包含 'Edg' 且排除 Safari)

    if (userAgent.indexOf('Edg') > -1 && userAgent.indexOf('Chrome') > -1 && userAgent.indexOf('Safari') > -1) {

      alert('Microsoft Edge');

      return 'Microsoft Edge';

    }


    // 检查 Chrome(只包含 'Chrome' 且没有 'Edg')

    if (userAgent.indexOf('Chrome') > -1 && userAgent.indexOf('Edg') === -1) {

      alert('Chrome');

      return 'Chrome';

    }


    // 检查 Firefox

    if (userAgent.indexOf('Firefox') > -1) {

      alert('Mozilla Firefox');

      return 'Mozilla Firefox';

    }


    // 其他浏览器

    return 'Unknown Browser';

  }


  // 调用函数检测浏览器类型

  const browser = detectBrowser();


  // 输出浏览器类型

  console.log(`当前浏览器是:${browser}`);

})();

</script>