JavaScript/jQuery - How to detect if a page is embedded by others?
Say I have a page index.html
, how can I detect, using JS or jQuery, if my page is embedded in other HTML page through a frame
or an iframe
?
+----------------------+
| Other people's site |
| +------------+ 开发者_JAVA技巧 |
| | index.html | |
| | oops! | |
| | embedded! | |
| +------------+ |
| |
+----------------------+
if(window.top !== window.self) {
// embedded
}
window.top
is the top-level window. window.self
is the window the script is executing in. If the current window is not the top-level window, it must be embedded.
精彩评论