Faster, better AI-powered code reviews. Start your free trial!  
Faster, better AI-powered code reviews.
Start your free trial!

Get high quality AI code reviews

JavaScript Get Hostname, Computer Name, or Machine Name

Table of Contents

JavaScript running in a browser operates in a sandboxed environment for security reasons. This sandboxing restricts JavaScript, preventing it from directly accessing system-level information, such as the computer or machine name. However, it can easily access the hostname, which refers to the domain name of the website being accessed.

The primary reason browsers run JavaScript in sandbox mode is to protect user privacy and security. This prevents malicious scripts from gathering potentially sensitive information without the user’s consent.

This article covers workarounds and best practices for obtaining the hostname and computer/machine name using JavaScript source code.

JavaScript Get Hostname

The hostname in a web environment signifies the domain from which the web page is served. Accessing it is straightforward using JavaScript’s window.location object. The hostname property of this object provides the domain name of the current page.

console.log("Hostname:", window.location.hostname);

This simple line of code can be instrumental in various scenarios, such as dynamically generating links, performing redirections based on the hostname, or implementing multi-tenant web applications where the content or configuration depends on the accessed domain.

window.location Properties in JavaScript

Understanding the window.location object in JavaScript is crucial for manipulating and retrieving information about the current page’s URL. Each property of window.location offers unique insights into the structure of the URL, enabling developers to tailor web applications based on the URL’s components. Below is a concise guide with examples for each relevant property, aimed at providing a quick reference for developers.

PropertyDescriptionExample Output
window.location.hostReturns the hostname and port of the URL.example.com:80
window.location.hostnameReturns the domain name of the URL.example.com
window.location.protocolReturns the protocol scheme of the URL.http:
window.location.portReturns the port number of the URL.80
window.location.pathnameReturns the path or pathname of the URL./path/page.html
window.location.originReturns the protocol, hostname, and port of the URL.http://example.com:80

This table offers a snapshot of how to use the window.location properties to extract specific parts of the current page’s URL. Whether you’re redirecting users, performing security checks, or just need to display part of the URL in your web application, these properties provide the tools you need to achieve your goals effectively.

Workarounds to Get Computer Name or Machine Name Using JavaScript

Although direct access is restricted, there are ways to work around these limitations for legitimate purposes. One effective method is to use Node.js or Electron to get the computer/machine name using the os module:

const os = require('os');
console.log("Computer Name:", os.hostname());

This approach is useful in desktop application and server-side applications where the computer name might be relevant for logging, configuration, or administrative purposes.

Best Practices to Get Computer Name Using JavaScript in Node.js and Electron

Retrieving the computer’s name is a common requirement in desktop applications developed with Node.js and Electron. This task can be accomplished through various methods, depending on the operating system. In this blog section, we will explore a practical approach to obtaining the computer name using JavaScript, focusing on Node.js and Electron environments. We will also discuss some best practices to ensure our code is efficient, reliable, and cross-platform compatible.

The provided code snippet is a well-rounded example of how to get the computer name in different operating systems:

const cp = require('child_process')
const os = require('os')

function getComputerName() {
  switch (process.platform) {
    case "win32":
      return process.env.COMPUTERNAME;
    case "darwin":
      return cp.execSync("scutil --get ComputerName").toString().trim();
    case "linux":
      const prettyname = cp.execSync("hostnamectl --pretty").toString().trim();
      return prettyname === "" ? os.hostname() : prettyname;
    default:
      return os.hostname();
  }
}

console.log("getComputerName", getComputerName());

Understanding the Approach

The function getComputerName uses Node.js’s built-in modules child_process and os to fetch the computer name. The method varies based on the operating system:

  • Windows (win32): It directly uses the COMPUTERNAME environment variable available in Windows environments.
  • macOS (darwin): Executes a shell command (scutil --get ComputerName) to get the computer name, which is more reliable than environment variables.
  • Linux: Tries to get a “pretty” hostname using hostnamectl --pretty. If it’s not set (empty string), it falls back to the standard hostname provided by the os module.

Advantages of Using getComputerName() Function

  1. Cross-Platform Compatibility: The code handles different operating systems by switching based on process.platform. This approach ensures compatibility across major platforms (Windows, macOS, Linux) and is a best practice for developing cross-platform applications.
  2. Using Native Node.js Modules: By leveraging child_process and os, the code sticks to native Node.js modules, avoiding external dependencies. This practice enhances performance and reliability.
  3. Error Handling: While the provided snippet is straightforward, incorporating try-catch blocks, especially around execSync calls, can prevent the application from crashing if the shell command fails or is unavailable.
  4. Security Considerations: Executing shell commands can expose your application to injection attacks if the commands include user input. In this case, the commands are hardcoded and safe, but always validate and sanitize input when building commands dynamically.
  5. Asynchronous Execution: The execSync method is used, which blocks the event loop until the command completes. For applications requiring high responsiveness or performing frequent calls, consider using exec (asynchronous) to avoid blocking.
  6. Fallback Strategies: The code smartly falls back to the standard hostname if specific methods (like hostnamectl --pretty on Linux) do not return a meaningful result. This resilience ensures that the function always returns a value.
  7. Testing Across Environments: Ensure your application is thoroughly tested on all target platforms. Behavior can vary, especially with command-line utilities and environment variables.
  8. Use in Electron: For Electron applications, this code can be used in the main process. If you need to access the computer name in the renderer process, consider using Electron’s ipcMain and ipcRenderer modules to send the data securely from the main process to the renderer.

Practical Applications of Getting the Hostname

Why bother fetching the hostname, you might wonder? The applications are as varied as they are vital. For instance, redirecting users to a different version of your site based on the domain, personalizing content to create a more engaging user experience, or setting up a multi-tenant application where the content is dynamically adjusted based on the hostname. Consider this snippet as a basic yet powerful demonstration of conditional operations based on the hostname:

if (window.location.hostname === "example.com") {
    console.log("This is the main site.");
} else {
    console.log("This is a subdomain or another domain.");
}

Handling Different Environments: Development vs. Production

A common challenge in web development is distinguishing between the development and production environments. The hostname can serve as a reliable indicator, with developers often using localhost for development. Here’s how you can leverage this distinction in your code:

if (window.location.hostname === "localhost") {
    console.log("Running in development mode.");
} else {
    console.log("Running in production mode.");
}

Advanced Usage: Parsing Hostname for Subdomains

For more complex applications, particularly those utilizing subdomains for structuring or organization, parsing the hostname to extract subdomain information can unlock a new level of customization. By dissecting the hostname, you can identify specific segments and tailor your logic accordingly:

const hostnameParts = window.location.hostname.split('.');
if (hostnameParts.length > 2) {
    console.log("Subdomain detected:", hostnameParts[0]);
} else {
    console.log("No subdomain detected.");
}

Cross-Origin Considerations

When dealing with hostnames, it’s crucial to remember the Same-Origin Policy, a security mechanism that restricts how a document or script loaded from one origin can interact with resources from another origin. This policy plays a pivotal role in how you can retrieve and use the hostname, especially in scenarios involving iframes or cross-origin requests. Navigating these waters requires a thorough understanding and sometimes innovative solutions to maintain functionality while ensuring security.

Sending the Computer or Machine Name to the Client-Side

For scenarios where the client-side needs to be aware of the computer or machine name, one can send this information from the server to the client. This can be done through HTTP headers, embedded in the web page, or via web sockets. However, it’s crucial to evaluate the necessity and security implications of exposing such information.

Example 1: Using HTTP Headers

In this example, a Node.js server adds a custom HTTP header containing the computer’s name when serving a web page. The client-side JavaScript then reads this header to obtain the computer name.

Server-side (Node.js):

const http = require('http');
const os = require('os');

const hostname = '127.0.0.1';
const port = 3000;

const server = http.createServer((req, res) => {
  // Set a custom header with the computer name
  res.setHeader('X-Computer-Name', os.hostname());

  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/html');
  res.end(`<html><body><script>
      // Client-side script to read the header is embedded here for simplicity
      fetch(window.location.href, { method: 'HEAD' })
        .then(response => {
          const computerName = response.headers.get('X-Computer-Name');
          console.log('Computer Name from Header:', computerName);
          // Optionally, display the computer name on the webpage
          document.body.innerHTML += '<p>Computer Name: ' + computerName + '</p>';
        });
      </script>Page content goes here.</body></html>`);
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

Client-side:

The client-side code is embedded in the server’s response above. It performs a fetch request to the same URL with the HEAD method to retrieve headers without downloading the entire body. Then, it accesses the X-Computer-Name header to get the computer name.

Example 2: Using Web Sockets

This example demonstrates setting up a WebSocket connection between the client and server. The server sends the computer’s name to the client through this persistent connection.

Server-side (Node.js) with WebSocket:

First, you’ll need to install the ws package for WebSocket functionality:

npm install ws

Then, create your server:

const WebSocket = require('ws');
const os = require('os');
const http = require('http');
const express = require('express');
const app = express();

// Serve an HTML file that establishes a WebSocket connection
app.get('/', (req, res) => {
  res.sendFile(__dirname + '/index.html');
});

const server = http.createServer(app);
const wss = new WebSocket.Server({ server });

wss.on('connection', function connection(ws) {
  console.log('Client connected');
  // Send the computer name to the client
  ws.send(JSON.stringify({ computerName: os.hostname() }));
});

const port = 3000;
server.listen(port, function listening() {
  console.log(`Listening on ${port}`);
});

Client-side (HTML/JavaScript):

Create an index.html file in the same directory as your server script:

<!DOCTYPE html>
<html>
<head>
    <title>WebSocket Example</title>
</head>
<body>
    <script>
        const socket = new WebSocket('ws://localhost:3000');

        socket.addEventListener('open', function (event) {
            console.log('Connected to WS Server');
        });

        socket.addEventListener('message', function (event) {
            const data = JSON.parse(event.data);
            console.log('Computer Name from WebSocket:', data.computerName);
            // Display the computer name on the webpage
            document.body.innerHTML += '<p>Computer Name: ' + data.computerName + '</p>';
        });
    </script>
</body>
</html>

In this WebSocket example, the server sends a message containing the computer name as soon as a client connects. The client then parses this message and logs the computer name or displays it on the webpage.

Both methods allow for the transfer of the computer or machine name from the server to the client but should be used judiciously with attention to privacy and security considerations.

Conclusion

In conclusion, fetching the hostname, computer name or machine name in JavaScript, Node.js and Electron applications is straightforward with the right approach and considerations. The provided code snippets is a solid starting point. By following best practices for cross-platform compatibility, security, and error handling, you can ensure that your application runs smoothly across all environments.

Picture of Nisha Kumari

Nisha Kumari

Nisha Kumari, a Founding Engineer at Bito, brings a comprehensive background in software engineering, specializing in Java/J2EE, PHP, HTML, CSS, JavaScript, and web development. Her career highlights include significant roles at Accenture, where she led end-to-end project deliveries and application maintenance, and at PubMatic, where she honed her skills in online advertising and optimization. Nisha's expertise spans across SAP HANA development, project management, and technical specification, making her a versatile and skilled contributor to the tech industry.

Written by developers for developers

This article was handcrafted with by the Bito team.

Latest posts

Mastering Python’s writelines() Function for Efficient File Writing | A Comprehensive Guide

Understanding the Difference Between == and === in JavaScript – A Comprehensive Guide

Compare Two Strings in JavaScript: A Detailed Guide for Efficient String Comparison

Exploring the Distinctions: == vs equals() in Java Programming

Understanding Matplotlib Inline in Python: A Comprehensive Guide for Visualizations

Top posts

Mastering Python’s writelines() Function for Efficient File Writing | A Comprehensive Guide

Understanding the Difference Between == and === in JavaScript – A Comprehensive Guide

Compare Two Strings in JavaScript: A Detailed Guide for Efficient String Comparison

Exploring the Distinctions: == vs equals() in Java Programming

Understanding Matplotlib Inline in Python: A Comprehensive Guide for Visualizations

Get Bito for IDE of your choice