Cross Domain Utils

2.0.38 · maintenance · verified Tue Apr 21

Cross Domain Utils is a JavaScript utility library designed to simplify and secure interactions between browser windows across different domains. It provides a robust set of functions for safely querying window properties like domain, parent, opener, and child frames, while mitigating common browser security restrictions (e.g., `SecurityError` exceptions). The current stable version for the unscoped `cross-domain-utils` package is 2.0.38, published approximately four years ago. For versions 3.0.0 and above, the package has been migrated to the `@krakenjs/cross-domain-utils` scope. This library is particularly useful for complex iframe communication, secure window management, and scenarios requiring precise control over cross-origin window relationships without triggering browser errors or warnings.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates key utilities like `getDomain`, `isSameDomain`, `getTop`, `getParent`, and `isWindowClosed` in both same-domain and simulated cross-domain scenarios, highlighting how the library handles security boundaries.

import { getDomain, isSameDomain, getTop, getParent, isWindowClosed } from 'cross-domain-utils';

function setupIframe(url) {
  return new Promise(resolve => {
    const iframe = document.createElement('iframe');
    iframe.src = url;
    iframe.onload = () => resolve(iframe);
    document.body.appendChild(iframe);
  });
}

async function runExample() {
  const currentWindow = window;
  console.log(`Current window domain: ${getDomain(currentWindow)}`);

  // Simulate a same-domain iframe
  const sameDomainIframe = await setupIframe(window.location.href);
  const sameDomainWin = sameDomainIframe.contentWindow;
  console.log(`Same-domain iframe loaded. Domain: ${getDomain(sameDomainWin)}`);
  console.log(`Is same domain as current: ${isSameDomain(sameDomainWin)}`);
  console.log(`Iframe's parent: ${getParent(sameDomainWin) === currentWindow ? 'current window' : 'other'}`);

  // Simulate a cross-domain iframe (requires a different origin, e.g., a local server on another port)
  // For demonstration, we'll just illustrate the conceptual call, as actual cross-domain setup is complex.
  // Replace with a real cross-origin URL for actual testing.
  const crossDomainUrl = 'http://localhost:8081/other-page.html'; // Example, needs a running server
  const crossDomainIframe = await setupIframe(crossDomainUrl);
  const crossDomainWin = crossDomainIframe.contentWindow;

  // Accessing cross-domain properties directly would throw SecurityError.
  // cross-domain-utils handles this gracefully.
  console.log(`
Attempting cross-domain checks...`);
  console.log(`Is cross-domain window closed: ${isWindowClosed(crossDomainWin)}`); // Safe check
  console.log(`Is cross-domain window on same domain: ${isSameDomain(crossDomainWin)}`); // Safe check

  try {
    getDomain(crossDomainWin);
  } catch (e) {
    console.warn(`getDomain on cross-domain window threw: ${e.message}`); // Expected behavior
  }

  const topWindow = getTop(currentWindow);
  console.log(`Top-level window is current window: ${topWindow === currentWindow}`);

  // Clean up iframes
  document.body.removeChild(sameDomainIframe);
  document.body.removeChild(crossDomainIframe);
}

runExample();

view raw JSON →