callback method in iframe to return a value to ope

2019-04-12 14:42发布

问题:

I have to call a callback method in iframe to return a value to opener.

I know SqueezeBox has "assign, open, close" static methods, but I do not understand how it works, can someone help me please?

回答1:

I don't know much about SqueezeBox but I have done some work with iframe communication. Unless your iFrame and opener are in the same domain, you cannot call from one to the other.

What I have done to work around this is write to the hash of the URL. The opener can then read this value and figure out what to do.

For example,

<iframe name="my-frame" id="my_frame" src="http://www.somewhere.com" width="540" height="1000" border="0" style="overflow:hidden; border: none;">
   <script type="text/javascript">window.location.hash = 'close';</script>
</iframe>

<script type="text/javascript">

  // Function to look for a token in the url hash 
  var tokenValue = function(){
    var hash = document.location.hash;
    return (hash && hash.length > 1) ? hash.substring(1, hash.length) : null;
  };

  // Function to set the token and notify the user when it is found in the url hash.
  var checkForToken = function(){
    if (tokenValue()) {
      alert(tokenValue());
      $clear(periodical);
    }
  };

  // Start a periodical that will check for
  var periodical = checkForToken.periodical(100);
</script>