简体   繁体   中英

IFrame get parameters from parent url on different domain

It seems this is not possible by default because I get error blocked a frame with origin x from accessing a frame with origin y. I know this is because of security but I would like to allow by partners to integrate my iframe. Iframe is on domain mysubdomain.mysite.com and contains search box. Once user has made search it has been redirected to page partners-domain-containing-search-results.com Search results are also on domain mysubdomain.mysite.com and I would like to read parameters from url (hash and query string parameters) but I can't because I can't read parent url parameters (from partners-domain-containing-search-results.com ).

You should be able to use top.location from subdomain.mysite.com to read the parent urls of partners-domain-containing-search-results.com:

eg from an iframe hosted on mysubdomain.mysite.com and embedded in partners-domain-containing-search-results.com?hello=true#blabla you should be able to run:

top.location.hash
>"#blabla"
top.location.search
>"?hello=true"

Note: you cannot modify the other domain's location object or you will see:

Uncaught SecurityError: Blocked a frame with origin " http://domain2.com " from accessing a frame with origin " http://domain.com ". Protocols, domains, and ports must match.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM