Access parent URL from iframe

后端 未结 16 1330
野性不改
野性不改 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:23

    I couldnt get previous solution to work but I found out that if I set the iframe scr with for example http:otherdomain.com/page.htm?from=thisdomain.com/thisfolder then I could, in the iframe extract thisdomain.com/thisfolder by using following javascript:

    var myString = document.location.toString();
    var mySplitResult = myString.split("=");
    fromString = mySplitResult[1];
    
    0 讨论(0)
  • 2020-11-22 10:28

    For pages on the same domain and different subdomain, you can set the document.domain property via javascript.

    Both the parent frame and the iframe need to set their document.domain to something that is common betweeen them.

    i.e. www.foo.mydomain.com and api.foo.mydomain.com could each use either foo.mydomain.com or just mydomain.com and be compatible (no, you can't set them both to com, for security reasons...)

    also, note that document.domain is a one way street. Consider running the following three statements in order:

    // assume we're starting at www.foo.mydomain.com
    document.domain = "foo.mydomain.com" // works
    document.domain = "mydomain.com" // works
    document.domain = "foo.mydomain.com" // throws a security exception
    

    Modern browsers can also use window.postMessage to talk across origins, but it won't work in IE6. https://developer.mozilla.org/en/DOM/window.postMessage

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

    If your iframe is from another domain, (cross domain), you will simply need to use this:

    var currentUrl = document.referrer;
    

    and - here you've got the main url!

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

    I just discovered a workaround for this problem that is so simple, and yet I haven't found any discussions anywhere that mention it. It does require control of the parent frame.

    In your iFrame, say you want this iframe: src="http://www.example.com/mypage.php"

    Well, instead of HTML to specify the iframe, use a javascript to build the HTML for your iframe, get the parent url through javascript "at build time", and send it as a url GET parameter in the querystring of your src target, like so:

    <script type="text/javascript">
      url = parent.document.URL;
      document.write('<iframe src="http://example.com/mydata/page.php?url=' + url + '"></iframe>');
    </script>
    

    Then, find yourself a javascript url parsing function that parses the url string to get the url variable you are after, in this case it's "url".

    I found a great url string parser here: http://www.netlobo.com/url_query_string_javascript.html

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

    In chrome it is possible to use location.ancestorOrigins It will return all parent urls

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

    This worked for me to access the iframe src url.

    window.document.URL
    
    0 讨论(0)
提交回复
热议问题