Skip to content

Instantly share code, notes, and snippets.

@sandcastle
Last active May 2, 2018 12:32
Show Gist options
  • Select an option

  • Save sandcastle/4269c1cab60a9e35304efe90394a3447 to your computer and use it in GitHub Desktop.

Select an option

Save sandcastle/4269c1cab60a9e35304efe90394a3447 to your computer and use it in GitHub Desktop.
Checks if a given window is within an iframe in a safe way.
/**
* Determines if the the current window is within an iframe
* in a safe cross browser model.
*
* @returns {Boolean} True if in an iframe, else false.
*/
inIframe() {
try {
return window.self !== window.top;
} catch (e) {
return true;
}
}
@sandcastle

Copy link
Copy Markdown
Author

Browsers can block access to window.top due to same origin policy. IE bugs also take place.

https://stackoverflow.com/a/326076/929771

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment