Access parent URL from iframe

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

提交回复
热议问题