作为一个页面,我可以告诉我是否已经使用javascript加载到iframe中?

rpo*_*agr 11 javascript jquery

我正在将页面加载到iframe中.两个页面都在同一个域中.我希望加载的页面只有在加载到iframe时才能执行特定的js功能.这可能吗?

额外奖励:可以在jQuery中完成吗?

谢谢

Kev*_*vin 22

要不就:

var isEmbed = window != window.parent; 
Run Code Online (Sandbox Code Playgroud)


Šim*_*das 9

可能是最简单的方法:

if ( self !== top ) {
    // you're in an iframe
}
Run Code Online (Sandbox Code Playgroud)

那么,你检查当前窗口是否是最顶层的窗口...