代码之家  ›  专栏  ›  技术社区  ›  VoodooChild

Javascript:从弹出窗口中找到主页上的框架?

  •  0
  • VoodooChild  · 技术社区  · 14 年前

    function sendRefreshMessage(data) {
        var myObj = null;
        myObj = document.getElementById('slPlugin');
        if (null != myObj) {
            try {
                //perform operation on myObj
            } catch (err) {
            }
        }
        else {
            if (null != top.opener.top.mainFrame) {
                myObj = top.opener.top.mainFrame.document.getElementById('slPlugin');
                if (null != myObj) {
                    try {
                        //perform operation on myObj
                    } catch (err) {
                    }
                }
            }
            else {
                myObj = top.opener.top.opener.top.mainFrame.document.getElementById('slPlugin');
                if (null != myObj) {
                    try {
                        //perform operation on myObj
                    } catch (err) {
                    }
                }
            }
        }
    }
    
    1 回复  |  直到 14 年前
        1
  •  1
  •   Frédéric Hamidi    14 年前

    更好的 )方法是,假设插件总是驻留在一个名为 mainFrame :

    function findPlugin(container)
    {
        var plugin = null;
        if (container.mainFrame != null) {
            plugin = container.mainFrame.document.getElementById('slPlugin');
        }
        if (plugin == null && container.opener != null) {
            plugin = findPlugin(container.opener.top);
        }
        return plugin;
    }
    
    function sendRefreshMessage(data)
    {
        var plugin = findPlugin(window.top);
        if (plugin != null) {
            try {
                // Perform operation on `plugin`.
            } catch (err) {
                // Please avoid empty catch blocks, they're evil.
            }
        }
    }