从 Windows 7 小工具中嵌入的 Iframe 中访问父 DOM/函数

发布于 2024-10-15 19:51:20 字数 240 浏览 6 评论 0原文

我有以下问题,我创建一个 Windows 7 小工具,它使用 Iframe 加载内容。我可以完全控制 Iframe 的内容,我想要做的是,调用父级中的函数(Windows 7 小工具 html 文档),从此 Iframe 内,甚至当鼠标悬停在链接或其他内容上时,从 Iframe 内触发弹出窗口。
非常感谢任何帮助。
谢谢
在此处输入图像描述

I have the following issue, Im creating a windows 7 gadget, which uses Iframe to load content.I have full control on the contents of the Iframe, what I want to do is, call a function in the parent (windows 7 gadget html document), from within this Iframe, or even trigger a flyout from within the Iframe, when there is hover on a link or something.
Any help is greatly appreciated.
Thanks
enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

过气美图社 2024-10-22 19:51:20

尽管最初据说 Windows 桌面小工具不受同源策略的限制,但仅适用于 XMLHttpRequest。如果 指向 www 上的页面,则框架页面与托管小工具之间的任何通信都将被阻止。如果是这种情况,那么您可能可以使用依赖于更改最顶层窗口的哈希的跨域通信方法。从框架内部,您可以执行如下操作:

window.top.location.hash = "#ShowFlyout";

然后,在小工具的代码中,您可以执行以下操作:

window.setInterval(function () {
    if (window.location.hash == "#ShowFlyout") {
        window.location.hash = "";

        System.Gadget.Flyout.file = "flyout.htm";
        System.Gadget.Flyout.show = true;
    }
}, 100);

I don't have my windows machine on hand to test it right now, but you can try it stillest 。

如果 iframe 指向本地计算机上的 html 文档,那么您应该能够作为最顶层窗口对象(即小工具)的成员来访问全局 System 变量,如下所示:

var System = window.top.System;
System.Gadget.Flyout.file = "some.htm";
System.Gadget.Flyout.show = true;

或者,还假设您可以控制弹出窗口的内容,您可以使用 jQuery 在所有链接上设置事件处理程序(因为您标记了它):

$("a", iframe.contentWindow.document).click(function () {
    System.Gadget.Flyout.file = this.href;
    System.Gadget.Flyout.show = true;
});

Although Windows Desktop Gadgets were initially said to be excluded from the restrictions of the Same Origin Policy, that is only true of XMLHttpRequests. If the <iframe> is pointing to a page on the www, then any communication between the framed page and the hosting gadget will be blocked. If this is the case then you might be able to use the method of cross-domain communication that relies on changing the hash of the topmost window. From inside the frame, you would do something like this:

window.top.location.hash = "#ShowFlyout";

Then, in the code for the gadget you'd have something like this:

window.setInterval(function () {
    if (window.location.hash == "#ShowFlyout") {
        window.location.hash = "";

        System.Gadget.Flyout.file = "flyout.htm";
        System.Gadget.Flyout.show = true;
    }
}, 100);

I don't have my windows machine on hand to test it right now, but you could try it nonetheless.

If the iframe is pointing to a html document on the local machine, then you should be able to access the global System variable as a member of the topmost window object — which is the gadget — like this:

var System = window.top.System;
System.Gadget.Flyout.file = "some.htm";
System.Gadget.Flyout.show = true;

Or, also assuming you have control over the content of the flyout, you could set an event handler on all links with jQuery (since you tagged it):

$("a", iframe.contentWindow.document).click(function () {
    System.Gadget.Flyout.file = this.href;
    System.Gadget.Flyout.show = true;
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文