简体   繁体   中英

Fancybox / iFrame location (cross domain) using postMessage

I am trying to grab the location of an iFrame (in a fancybox) when the user clicks a button in the title area. It's on a different domain so I'm using postMessage, and I want it to grab the full url (eg "www.example.com?pid=12345") so when the fancybox is closed using the "Attempt Search" button, my site can then search for the pid "12345".

Snippets...

<script type="text/javascript">
  window.addEventListener("message", function(e) {
    alert(e.data + " : " + e.origin + " : " + e.source);
  }, false);
</script>


<script type="text/javascript">
  $(document).ready(function() {
    $("a#codelookup").fancybox({
    'width' : 1050,
    'height' : '75%',
    'title' : '<p class="wrapbutton"><a href="javascript:;" onClick="postMessage(\'Hello?\', \'*\'); $.fancybox.close();">Attempt Search</a></p>',
    'titlePosition' : 'inside',
    'type' : 'iframe'
  });
});

This alerts the "Hello?" message OK, but e.origin returns the parent domain (not the iFrame's url). And if I postMessage(this.location, '*') it's undefined.

Can anybody help please?

It is not possible. For security reasons, an iframe of a different domain can not communicate with the page that it is being framed in on.

Have you tried this library? http://easyxdm.net/wp/

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