HTML Window self Property

The HTML Window self property returns a reference to the current window object. It is essentially the same as the global window object and is commonly used to check if the current window is the topmost window or if it's embedded within a frame or iframe.

Syntax

Following is the syntax for accessing the window self property −

window.self

The window.self property returns the window object itself, making it equivalent to simply using window.

Return Value

The window.self property returns the current window object reference.

Common Use Cases

The window self property is primarily used for −

  • Frame detection − Checking if the current window is the top-level window or embedded in a frame/iframe.

  • Window comparison − Comparing the current window with parent or top windows.

  • Security checks − Preventing clickjacking attacks by ensuring the page is not loaded in a frame.

Example − Basic Window Self Property

Following example demonstrates the basic usage of window self property −

<!DOCTYPE html>
<html>
<head>
   <title>Window Self Property</title>
</head>
<body style="font-family: Arial, sans-serif; padding: 20px; text-align: center;">
   <h1>Window Self Property Demo</h1>
   <button onclick="checkSelf()" style="padding: 10px 20px; font-size: 16px; cursor: pointer;">Check Window Self</button>
   <p id="result"></p>
   <script>
      function checkSelf() {
         var result = document.getElementById("result");
         result.innerHTML = "window.self === window: " + (window.self === window);
      }
   </script>
</body>
</html>

The output shows that window.self is identical to the window object −

window.self === window: true

Example − Checking Top-Level Window

Following example shows how to use window.self to check if the current window is the topmost window −

<!DOCTYPE html>
<html>
<head>
   <title>Window Self vs Top</title>
</head>
<body style="font-family: Arial, sans-serif; padding: 20px; text-align: center;">
   <h1>HTML Window Self Property Demo</h1>
   <button onclick="checkTopWindow()" style="padding: 10px 20px; font-size: 16px; cursor: pointer; background: #007bff; color: white; border: none; border-radius: 4px;">Check if Topmost Window</button>
   <div id="output" style="margin-top: 20px; font-size: 18px; font-weight: bold;"></div>
   <script>
      function checkTopWindow() {
         var outputDiv = document.getElementById("output");
         if (window.top === window.self) {
            outputDiv.innerHTML = "The current window is the topmost window";
            outputDiv.style.color = "green";
         } else {
            outputDiv.innerHTML = "The current window is not the topmost window";
            outputDiv.style.color = "red";
         }
      }
   </script>
</body>
</html>

When run in a normal browser window, this will show that the current window is the topmost window −

The current window is the topmost window (in green)

Example − Frame Detection

Following example demonstrates how to detect if a page is loaded inside a frame using window.self −

<!DOCTYPE html>
<html>
<head>
   <title>Frame Detection</title>
</head>
<body style="font-family: Arial, sans-serif; padding: 20px;">
   <h1>Frame Detection Demo</h1>
   <button onclick="detectFrame()" style="padding: 10px 15px; font-size: 16px; cursor: pointer;">Detect Frame Status</button>
   <div id="frameStatus" style="margin-top: 15px; padding: 10px; border: 1px solid #ccc; border-radius: 4px;"></div>
   <script>
      function detectFrame() {
         var status = document.getElementById("frameStatus");
         if (window.self !== window.top) {
            status.innerHTML = "?? This page is loaded inside a frame or iframe";
            status.style.backgroundColor = "#fff3cd";
            status.style.color = "#856404";
         } else {
            status.innerHTML = "? This page is not loaded inside a frame";
            status.style.backgroundColor = "#d4edda";
            status.style.color = "#155724";
         }
      }
      
      // Check frame status on page load
      window.onload = function() {
         detectFrame();
      };
   </script>
</body>
</html>

This example automatically checks the frame status when the page loads and provides a visual indication.

Window Object Hierarchy window.top Topmost window window.parent Parent window window.self Current window window.self === window Always returns true

Window Self vs Other Window Properties

Following table compares window.self with other related window properties −

Property Description Use Case
window.self Reference to the current window (same as window) Frame detection, window comparisons
window.top Reference to the topmost window in the hierarchy Accessing the main browser window
window.parent Reference to the parent window (or self if top-level) Communication between frames
window The global window object Accessing browser APIs and global scope

Practical Applications

The window.self property is particularly useful for −

  • Security − Preventing clickjacking by ensuring pages are not embedded in malicious frames.

  • Responsive design − Adjusting behavior based on whether content is in a frame or standalone window.

  • Cross-frame communication − Identifying the current window context in multi-frame applications.

Conclusion

The window.self property provides a reference to the current window object, making it useful for frame detection and window comparisons. While functionally equivalent to the global window object, it serves as an explicit way to reference the current window context, particularly when working with frames and iframes.

Updated on: 2026-03-16T21:38:54+05:30

212 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements