Urgent.News

What's breaking now, across thousands of outlets.

Tech

How to Get the Parent Domain from a Cross-Origin Iframe in JavaScript (Without Losing Your Sanity)

1. Introduction If you are developing an iframe-based widget, embedded tool, or third-party script, you will inevitably run into a question that sounds deceptively simple: "Which website embedded my iframe?" Knowing the parent domain is essential for domain authorization, analytics, and security origin checks. But here is the catch: modern web browsers trust nobody — especially not your iframe.…

Getting the parent domain from a cross-origin iframe in JavaScript can be tricky. Modern web browsers enforce the Same-Origin Policy (SOP), preventing iframes from accessing each other's URLs, DOM, or cookies. This means you cannot simply read window.parent.location.href across origins. Additionally, document.referrer may be stripped by websites, either through a meta tag or HTTP headers. This article covers three methods to reliably obtain the top-level domain of an iframe:

1. Same-Origin Path: If your iframe is a child of the same domain, you can access its URL directly using window.location.href or window.location.origin.

2. Referrer Policy: Try reading document.referrer, but be aware that websites can disable the referrer header entirely, resulting in an empty string.

3. Ancestor Origins: Use window.location.ancestorOrigins, which returns an array of all parent origins. By accessing ancestorOrigins[ancestorOrigins.length - 1], you can retrieve the domain of the top-level page.

A more robust solution is to implement a multi-tier function called getTopParentDomain(). This function attempts to determine the top-level domain using various strategies in the following order: direct access with window.location.href, parsing document.referrer when referrer policies allow, and finally, checking the ancestorOrigins array. Each step includes error handling and fallback mechanisms to gracefully handle unsupported browsers or stripped referrer information.

This approach ensures your JavaScript application can retrieve the parent domain even in complex iframe scenarios, providing a reliable way to implement domain checks, analytics, and security measures without compromising user privacy or encountering browser restrictions.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

This story

This is one outlet's version. Read the fullest account.

Read the original at dev.to →

More in Tech

How to record Google Meet and Zoom calls with dual audio using the HTML5 MediaRecorder API

Recording virtual meetings like Google Meet or Zoom often comes with annoying hurdles: 5-minute freemium limits, forced software installations, or needing explicit host permission just to record local…

  • HTML5 MediaRecorder API captures dual audio for Google Meet/Zoom
  • navigator.mediaDevices.getDisplayMedia captures screen video and system audio
  • MediaRecorder encodes video and audio into downloadable WebM/MP4 file

My screener recommended every job it could not read

This is a submission for DEV's Summer Bug Smash: Clear the Lineup powered by Sentry . Project Overview Today I wrote a screener for a freelance job board.

  • Failed fetches mistakenly endorsed unreadable jobs
  • Tool modified to flag HTTP errors, 404s, and rate-limited responses
  • Parser verified job postings before adding to shortlist

Sharing memory between processes with java.lang.foreign and jextract

Java Foreign Function and Memory API ( java.lang.foreign ) is a powerful tool that you can use to take advantage of various OS-specific optimizations.

  • java.lang.foreign simplifies OS-specific optimizations for Java code
  • jextract generates Java code from C/C++ header files
  • Short-circuit read/write uses shared memory for zero-copy data locality

More from Saturday 22 August →