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

发布于 2024-09-30 15:43:04 字数 930 浏览 0 评论 0原文

在下面的 Javascript 中,我必须不断从弹出页面中查找 mainFrame,有没有更好的方法来做到这一点?

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

In the following Javascript I have to keep finding the mainFrame from the Popup pages, is there a better way to do this?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟燃烟灭 2024-10-07 15:43:04

好吧,有一种更干净(但不一定更好)的方法来做到这一点,假设您的插件始终驻留在名为 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.
        }
    }
}

Well, there's a cleaner (but not necessarily better) way to do that, assuming your plugin always resides inside an element called 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.
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文