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

二次信任 提交于 2020-01-13 10:05:40

问题


I have googled as much as I can and I'm not sure of the support for the document of an iframe getting the id of itself in the parent window: window.frameElement.id. There are so many browsers that it's hard to test them all and nothing online seems to have any information. I'm specifically wondering about:

Safari iOS Safari Windows Phone IE IE 7, 8, 9, 10, 11

(I tested Firefox and Chrome and they both worked).

EXAMPLE IFRAME CONTENT

<!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>

回答1:


I happened to look for the same information. Here's what I have found.

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

I have tested some browsers in addition to the MDN doc, and confirmed that it is supported by IE7+, Safari, Firefox, Chrome. So pretty much all browsers.

I haven't tested on IE6-, but I guess you don't need to care.




回答2:


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

This code not working for me in cross browser (FF,Chrome,IE) A prefer this one, working ok and cross browser...

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


来源:https://stackoverflow.com/questions/23271268/is-window-frameelement-for-same-origin-iframes-supported-across-all-browsers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!