callback method in iframe to return a value to opener

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-12 09:11:24

问题


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>


来源:https://stackoverflow.com/questions/2070774/callback-method-in-iframe-to-return-a-value-to-opener

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!