Access parent URL from iframe

后端 未结 16 1269
野性不改
野性不改 2020-11-22 10:00

Okay, I have a page on and on this page I have an iframe. What I need to do is on the iframe page, find out what the URL of the main page is.

I have searched aroun

相关标签:
16条回答
  • 2020-11-22 10:36

    The problem with the PHP $_SERVER['HTTP_REFFERER'] is that it gives the fully qualified page url of the page that brought you to the parent page. That's not the same as the parent page, itself. Worse, sometimes there is no http_referer, because the person typed in the url of the parent page. So, if I get to your parent page from yahoo.com, then yahoo.com becomes the http_referer, not your page.

    0 讨论(0)
  • 2020-11-22 10:38

    You're correct. Subdomains are still considered separate domains when using iframes. It's possible to pass messages using postMessage(...), but other JS APIs are intentionally made inaccessible.

    It's also still possible to get the URL depending on the context. See other answers for more details.

    0 讨论(0)
  • 2020-11-22 10:38

    I've had issues with this. If using a language like php when your page first loads in the iframe grab $_SERVER['HTTP_REFFERER'] and set it to a session variable.

    This way when the page loads in the iframe you know the full parent url and query string of the page that loaded it. With cross browser security it's a bit of a headache counting on window.parent anything if you you different domains.

    0 讨论(0)
  • 2020-11-22 10:38

    Get All Parent Iframe functions and HTML

    var parent = $(window.frameElement).parent();
            //alert(parent+"TESTING");
            var parentElement=window.frameElement.parentElement.parentElement.parentElement.parentElement;
            var Ifram=parentElement.children;      
            var GetUframClass=Ifram[9].ownerDocument.activeElement.className;
            var Decision_URLLl=parentElement.ownerDocument.activeElement.contentDocument.URL;
    
    0 讨论(0)
提交回复
热议问题