从Windows 7小工具中嵌入的Iframe中访问父DOM /函数 [英] Accessing parent DOM/function from within an Iframe embedded in Windows 7 gadget

查看:120
本文介绍了从Windows 7小工具中嵌入的Iframe中访问父DOM /函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下问题,我正在创建一个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

推荐答案

虽然最初声称Windows桌面小工具被排除在同源策略,仅适用于XMLHttpRequests。如果< iframe> 指向www上的某个页面,则会阻止该框架页面与托管小工具之间的任何通信。如果是这种情况,那么可能能够使用依赖于更改最顶层窗口的哈希的跨域通信方法。从框架内部,您可以执行以下操作:

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);

我手边没有自己的Windows机器来测试它,但你可以尝试一下尽管如此。

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

如果iframe指向本地计算机上的html文档,那么您应该能够访问全局系统变量作为最顶层窗口对象的成员—这是小工具—像这样:

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;

或者,假设你可以控制弹出窗口的内容,你可以设置一个事件处理程序所有与jQuery的链接(因为你标记它):

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;
});

这篇关于从Windows 7小工具中嵌入的Iframe中访问父DOM /函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆