Javascript: найти кадр на главной странице из всплывающих окон? - PullRequest
0 голосов
/ 05 ноября 2010

В следующем Javascript я должен постоянно находить основной фрейм из всплывающих страниц, есть ли лучший способ сделать это?

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 Ответ

1 голос
/ 13 ноября 2010

Ну, есть более чистый (но не обязательно лучший ) способ сделать это, предполагая, что ваш плагин всегда находится внутри элемента, называемого 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.
        }
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...