所有浏览器都支持window.frameElement(用于同源iframe)

Is window.frameElement (for same-origin iframes) supported across all browsers?

本文关键字:用于 iframe frameElement 浏览器 支持 window      更新时间:2023-09-26

我已经尽可能多地在谷歌上搜索了,我不确定是否支持iframe的文档在父窗口中获取自己的id:window.frameElement.id。浏览器太多了,很难全部测试,而且网上似乎没有任何信息。我特别想知道:

SafariiOS SafariWindows Phone IE即7、8、9、10、11

(我测试了Firefox和Chrome,它们都能正常工作)。

示例IFRAME内容

<!DOCTYPE HTML>
<html>
    <head></head>
<body>
        <script type="text/javascript">
            var owner = window.frameElement;
            var thisIsWhoIsCalling = ( owner !== null ) ? owner.id : null;
            window.parent.SomeJavaScriptObject.someFunction( thisIsWhoIsCalling );
        </script>
    </body>
</html>

我碰巧在寻找相同的信息。以下是我的发现。

https://developer.mozilla.org/en-US/docs/Web/API/Window.frameElement

除了MDN文档之外,我还测试了一些浏览器,并确认IE7+、Safari、Firefox、Chrome都支持它。所以几乎所有的浏览器。

我还没有测试IE6-,但我想你不需要在意。

var frame = window.frameElement;  //Get <iframe> element of the window
if (frame) { window.location.href = "/403.shtml"; ...

此代码在跨浏览器(FF、Chrome、IE)中不适用于我A更喜欢这个,工作正常,跨浏览器。。。

  if (window.self === window.top) window.location.href = "/403.shtml";
//or if (window.self !== window.top) window.location.href = "/403.shtml";