Accessing parent DOM/function from within an Iframe embedded in Windows 7 gadget

[亡魂溺海] 提交于 2019-12-11 06:36:43

问题


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


回答1:


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


来源:https://stackoverflow.com/questions/4884676/accessing-parent-dom-function-from-within-an-iframe-embedded-in-windows-7-gadget

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