Home > Web Front-end > JS Tutorial > How to check which browser the current running environment is in JavaScript?

How to check which browser the current running environment is in JavaScript?

王林
Release: 2023-09-04 19:37:02
forward
1323 people have browsed it

How to check which browser the current running environment is in JavaScript?

Runtime environment is the environment in which code is executed. It tells your code which global objects it can access and also affects its results. JavaScript code can run in different types of environments, some of which are Node.js, Service Workers, or web browsers. So, to start coding with JavaScript, you don’t need to install any additional software. Every web browser comes with a JavaScript engine. You can simply run the script you write in any browser, but it ensures that it follows all the rules of ECMAScript (ES6) functionality.

Here we will detect which runtime environment our code is running in. JavaScript code written in Node.js can also run in any environment, whether it's a browser environment, a Service Worker environment, or the Node.js environment itself. When running code in a different environment, you need to match all the requirements of that environment.

Check whether the runtime environment is a browser

Tocheck whether the runtime environmentpiece of code is a browserThere is no direct way. So, to check the runtime environment, we have to set some conditions to match each environment and check in which environment our code is running. 一段代码是不是浏览器没有直接的方法。因此,要检查运行时环境,我们必须设置一些条件来匹配每个环境,并检查我们的代码在哪个环境中运行。

Syntax

The following is the syntax to check whether the current running environment is a browser-

type of window === "object"
Copy after login

If the above statement returns true, the current running environment is a browser, otherwise no.

Algorithm

  • Step 1< /strong> - Check condition typeof window === "object".
  • STEP 2 - If true is returned, a message is displayed because the current runtime environment is a window.
  • STEP 2 - If false is returned, a message is displayed because the current runtime environment is not a window.

Example

In the following example, we check whether the current running environment is a browser.

<!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>
Copy after login

Check different runtime environments

Each environment has different conditions.

  • For browser environments< /strong>, the type of window should equal "object".

  • For node.js environment, we must check 2 conditions. First, check whether the type of process is "object", and whether the type of require is "function" ".

  • The environment is node.js only if both conditions are true environment.

  • For Service Worker Environment, we check if the type of the imported script is equal to "function". When it is equal to a function, then only that environment is a service worker thread environment.

Syntax

The following is the syntax to check the runtime environment -

// 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"
Copy after login

Algorithm

  • 1st Step - First we check if the runtime environment is Node.js. If true, the correct message is displayed.
  • Step 2 - Next we check if the current runtime environment is a Service Worker. If true, the correct message is displayed.
  • Step 3 - Finally we check if the runtime environment is a browser. If true, the correct message is displayed.

Example

We can use the following code to check the runtime environment of the program.

<!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>
Copy after login

After clicking the "Check Runtime Environment" button, the screen will show you the output based on the environment in which the program is running.

This feature of JavaScript allows you to write code in any environment and run it in any other different environment, especially in a web browser, while using web pages that run only in a web browser.

Note- The method type used here will give us the data type of the variable, function or method just like it would be in string, number, object, function or any other type of data Type provides the same output.

The above is the detailed content of How to check which browser the current running environment is in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template