JavaScript Location protocol Property

The location.protocol property in JavaScript returns the protocol scheme of the current URL, including the colon (:). This property is useful for determining whether a page is loaded over HTTP, HTTPS, or other protocols.

Syntax

location.protocol

Return Value

Returns a string representing the protocol scheme of the URL, including the trailing colon. Common values include:

  • "https:" - Secure HTTP protocol
  • "http:" - Standard HTTP protocol
  • "file:" - Local file protocol
  • "ftp:" - File Transfer Protocol

Example

<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <title>Location Protocol Property</title>
   <style>
      body {
         font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
         padding: 20px;
      }
      .result {
         font-size: 18px;
         font-weight: 500;
         color: #2c3e50;
         margin: 10px 0;
      }
      .btn {
         background-color: #3498db;
         color: white;
         padding: 10px 20px;
         border: none;
         border-radius: 4px;
         cursor: pointer;
         font-size: 16px;
      }
      .btn:hover {
         background-color: #2980b9;
      }
   </style>
</head>
<body>
   <h1>Location Protocol Property</h1>
   <div class="result"></div>
   <button class="btn">Get Protocol</button>
   <p>Click the button to display the current page's protocol.</p>

   <script>
      const resultDiv = document.querySelector(".result");
      const button = document.querySelector(".btn");

      button.addEventListener("click", function() {
         const protocol = location.protocol;
         resultDiv.innerHTML = `Current protocol: <strong>${protocol}</strong>`;
         
         // Additional protocol information
         if (protocol === "https:") {
            resultDiv.innerHTML += "<br>? This is a secure connection";
         } else if (protocol === "http:") {
            resultDiv.innerHTML += "<br>? This is an unsecured connection";
         }
      });
   </script>
</body>
</html>

Common Use Cases

The location.protocol property is commonly used for:

  • Security checks: Ensuring pages are loaded over HTTPS
  • API calls: Making protocol-relative requests
  • Conditional loading: Loading different resources based on protocol

Example: Protocol-Based Conditional Logic

<!DOCTYPE html>
<html>
<head>
   <title>Protocol Check</title>
</head>
<body>
   <div id="status"></div>
   
   <script>
      const statusDiv = document.getElementById("status");
      
      if (location.protocol === "https:") {
         statusDiv.innerHTML = "? Secure connection detected";
         statusDiv.style.color = "green";
      } else {
         statusDiv.innerHTML = "?? Insecure connection - consider using HTTPS";
         statusDiv.style.color = "orange";
      }
      
      console.log("Current protocol:", location.protocol);
   </script>
</body>
</html>

Browser Compatibility

The location.protocol property is supported in all modern browsers and has been part of JavaScript since the early versions. It works consistently across all platforms.

Conclusion

The location.protocol property provides a simple way to determine the protocol scheme of the current URL. It's particularly useful for security checks and conditional logic based on whether the connection is secure or not.

Updated on: 2026-03-15T23:18:59+05:30

236 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements