How to prevent JavaScript from running inside iframe

June 16, 2017

There are some scenarios when we want to prevent JavaScript from running inside of the iframe.

Most common one Chrome/Safari extension’s injected script.

It runs multiple times if we didn’t prevent it.

Here’s a quick solution

if (window == window.top) { // do stuff }

That condition will fail if we are using inside an iframe.


👋 I'm Ashik Nesin • Sr. Frontend Engineer @Chargebee 👨‍💻• I write about modern Javascript & building web apps

© 2021 Nesin Technologies LLP