Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 566
DRC Avatar asked Dec 14 '25 14:12

DRC


1 Answers

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.

like image 131
Jason Small Avatar answered Dec 17 '25 05:12

Jason Small