Published on

How to prevent JavaScript from running inside iframe

Authors

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.