简体   繁体   中英

JavaScript alternative for Chrome, regarding window.parent

The following code is executed from a Silverlight application, which is running in an iframe, which is in a .aspx file:

    private void PostMessage<T>(Message<T> message)
    {
        HtmlWindow window = HtmlPage.Window;
        ScriptObject postMessage = window.Eval("window.parent.postMessage") as ScriptObject;

        if (postMessage != null)
        {
            string jsonMessage = ToJsonString(message);
            string[] args = { jsonMessage, "*" };
            postMessage.InvokeSelf(args);
        }
    }

This works fine in IE, but not in Google Chrome. This has to do with the window.parent reference, Chrome returns undefined. When the code is called in Chrome, it will crash: "Failed to invoke self"; the stacktrace starts with "at System.Windows.Browser.ScriptObject.InvokeSelf(Object[] args)"

Can anyone tell me how to rewrite this code to support Google Chrome too?

这应该工作:

window.frameElement.ownerDocument.parentWindow 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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