Fancybox / iFrame location (cross-domain) using postMessage

I am trying to capture the location of an iFrame (in fancybox) when the user clicks a button in the header area. This is in a different domain, so I use postMessage and I want it to capture the full URL (for example, "www.example.com?pid=12345"), so when fancybox closes using the "Search for purchases" button, the site can then find the pid "12345".

Excerpts ...

<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 warns "Hello?". OK message, but e.origin returns the parent domain (not the iFrame URL). And if I postMessage (this.location, '*'), it is undefined.

Can anyone help?

+3
source share
2 answers

. iframe , .

+1

All Articles