Accessing parent DOM/function from within an Ifram

2019-07-31 21:55发布

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

1条回答
我命由我不由天
2楼-- · 2019-07-31 22:45

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;
});
查看更多
登录 后发表回答