简体   繁体   English

我可以将hashChange侦听器设置为iFrame src吗?

[英]Can I set a hashChange listener to an iFrame src?

I'm wondering if I have a page, that contains multiple iFrames and if I'm communicating through modification of the iFrame src attribute, would it be possible to set a hashChange listener on the src attribute. 我想知道我是否有一个包含多个iFrames的页面,如果我通过修改iFrame src属性进行通信,是否可以在src属性上设置hashChange监听器。

Of course this would have to be inside the iFrame code, but trying this: 当然这必须在iFrame代码中,但尝试这个:

  $(document).ready(function () {  
    console.log(window);
    $(window).on('hashchange', function() {
      console.log("IFRAME HASH CHANGED");
    });
   });

Does not trigger anything. 不会触发任何事情。

Question: 题:
Any idea if this is possible? 知道这是否可行? If not, how else could it be done? 如果没有,怎么办呢?

Thanks for inputs! 感谢您的投入!

The code above is fine. 上面的代码很好。 You should have no issues setting a hashchange event listener on the iframe's window. 在iframe的窗口上设置hashchange事件侦听器应该没有问题。 The problem is that you are viewing the console for the parent window. 问题是您正在查看父窗口的控制台。 For example, if you change your console.log to an alert, you will see that it is in fact working. 例如,如果将console.log更改为警报,您将看到它实际上正在运行。

If you're using google chrome's developer tools, you can click on this drop-down to switch between which window context you are in: 如果您使用的是谷歌浏览器的开发人员工具,则可以单击此下拉列表在您所在的窗口上下文之间切换:

chrome developer tools的屏幕截图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM