JavaScript中如何检查当前运行环境是浏览器?

王林
王林 转载
2023-09-04 19:37:02 740浏览

JavaScript中如何检查当前运行环境是浏览器?

运行时环境是执行代码的环境。它告诉您的代码可以访问哪些全局对象,并且还会影响其结果。 JavaScript 代码可以在不同类型的环境中运行,其中一些是 Node.js、Service Workers 或 Web 浏览器。因此,要开始使用 JavaScript 编码,您无需安装任何其他软件。每个 Web 浏览器都带有 JavaScript 引擎。您可以简单地在任何浏览器中运行您编写的脚本,但它确保它遵循 ECMAScript (ES6) 功能的所有规则。

在这里,我们将检测我们的代码在哪个运行时环境中运行。用 Node.js 编写的 JavaScript 代码也可以在任何环境中运行,无论是浏览器环境、Service Worker 环境还是 Node.js 环境本身。在不同的环境中运行代码时,您需要匹配该环境的所有需求。

检查运行时环境是否是浏览器

检查运行时环境是否一段代码是不是浏览器没有直接的方法。因此,要检查运行时环境,我们必须设置一些条件来匹配每个环境,并检查我们的代码在哪个环境中运行。

语法

以下是检查是否运行的语法当前运行环境是否是浏览器 -

type of window === "object"

如果上述语句返回 true,则当前运行环境是浏览器,否则不是。

算法

  • 第 1 步 - 检查条件 typeof window === "object"。
  • STEP 2 - 如果返回 true,则显示一条消息,因为当前运行时环境是一个窗口。
  • STEP 2 - 如果返回 false,则显示一条消息,因为当前运行时环境不是窗口。

示例

在下面的示例中,我们检查当前运行环境是否是浏览器。

<!DOCTYPE html>
<html>
<body>
   <div>
   <h2>Check if the Current Runtime Environment is Browser</h2>
   <p>Click the below button to know if the runtime environment is browser or not</p>
   <button onclick = "isBrowser()"> Check Runtime Environment </button>
   <p id="result1"></p>
   <p id="result2"></p>
   </div>
   <script>
      function isBrowser() {
         var text="Runtime environment";

         // Check if the runtime environment is a Browser
         if (typeof window === "object") {
            document.getElementById("result1").innerHTML = text + " is Browser";
         } else {
            document.getElementById("result2").innerHTML = text + " is NOT Browser";
         }
      }
   </script>
</body>
</html>

检查不同的运行时环境

每个环境都有不同的条件。

  • 对于浏览器环境,窗口的类型应该等于“对象”。

  • 对于 node.js 环境,我们必须检查 2 个条件首先是检查进程的类型是否为“对象”,并且 require 的类型是否为“函数”。

  • 只有当这两个条件都为真时,环境才是节点.js 环境。

  • 对于服务工作者环境,我们检查导入脚本的类型是否等于“函数”当它等于一个函数时,则只有该环境是服务工作线程环境。

语法

以下是检查运行时环境的语法 -

// Condition if Runtime Environment is Node.js
typeof process === "object" &&typeof require === "

// Condition if Runtime Environment is Service Worker
typeof importScripts === "function

// Condition if Runtime Environment is Browser
typeof window === "object"

算法

  • 第1步 - 首先我们检查运行时环境是否为Node.js。如果为 true,则显示正确的消息。
  • 第 2 步 - 接下来我们检查当前运行时环境是否为 Service Worker。如果为 true,则显示正确的消息。
  • 第 3 步 - 最后我们检查运行时环境是否为浏览器。如果为true,则显示正确的消息。

示例

我们可以使用下面的代码来检查程序的运行时环境。

<!DOCTYPE html>
<html>
<body>
   <div>
   <h2>Check the Current Runtime Environment</h2>
   <p>Click the below button to know the runtime environment</p>
   <button onclick = "isBrowser()"> Check Runtime Environment </button>
   <p id="result1"></p>
   <p id="result2"></p>
   <p id="result3"></p>
   </div>
   <script>
      function isBrowser() {
         var text="Runtime environment";

         // Check if the runtime environment is Node.js
         if (typeof process === "object" &&typeof require === "function") {
            document.getElementById("result1").innerHTML = text + " is node.js"; }

            // Check if the runtime environment is a Service worker

            if (typeof importScripts === "function") {
               document.getElementById("result2").innerHTML = text + " is service worker";
            }

            // Check if the runtime environment is a Browser
            if (typeof window === "object") {
               document.getElementById("result3").innerHTML = text + " is Browser";
            }
         }
   </script>
</body>
</html>

单击“检查运行时环境”按钮后,屏幕将根据程序运行的环境向您显示输出。

JavaScript 的此功能允许您在任何环境中编写代码并在任何其他不同环境中运行它,特别是在网络浏览器中,同时使用仅在网络浏览器中运行的网页。

注意 - 这里使用的方法类型将为我们提供变量、函数或方法的数据类型,就像它以字符串、数字、对象、函数或任何其他类型的数据类型提供输出一样。

以上就是JavaScript中如何检查当前运行环境是浏览器?的详细内容,更多请关注php中文网其它相关文章!

声明:本文转载于:tutorialspoint,如有侵犯,请联系admin@php.cn删除