Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

273
Views
Detect top-level iframe in a nested iframes web page

I'm using a Chrome extension to inject web pages as iframes inside of one of my extension's internal pages.

Stripping the cross-origin headers from the iframes, so, they're showing just fine.

And setting the field "all_frames": true at the extension's manifest, which allows my content-script to be injected in all the pages and the iframes inside of them as well.

manifest.json :

"content_scripts": [
    {
        "matches": [
            "<all_urls>"
        ],
        "all_frames": true, //<------
        "run_at": "document_idle",
        "js": [
            "js/content-script.js"
        ]
    }
],

The problem is:

This injects my content-script at ALL the iframes inside the web pages (some websites has iframes at them already, so, we get a nested iframes situation here)..

I need to exchanges messages with only the top-level iframe. I don't care about the nested children iframes inside of the page.

I tried to detect the parent window like this:

content-script.js

if (window !== window.top) {
    console.console('iframe');
} else {
    console.console('NOT iframe');
}

But, this is always iframe, because, well, I'm injecting the top-level web page itself as an iframe inside of my extension.

My question is:

How can I detect the top-level iframe only?

So, if this is the case now:

<iframe name="1">
    <iframe name="2">
        <iframe name="3"></iframe>
    </iframe>
</iframe>

How can I detect if this script is running inside of iframe #2 ?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!