Javascript:从弹出窗口在主页上查找框架?
在下面的Javascript中,我必须不断从弹出页面中查找大型机,有更好的方法吗Javascript:从弹出窗口在主页上查找框架?,javascript,asp.net,frames,Javascript,Asp.net,Frames,在下面的Javascript中,我必须不断从弹出页面中查找大型机,有更好的方法吗 function sendRefreshMessage(data) { var myObj = null; myObj = document.getElementById('slPlugin'); if (null != myObj) { try { //perform operation on myObj } catch (err)
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) {
}
}
}
}
}
好吧,有一种更干净(但不一定更好)的方法可以做到这一点,假设您的插件总是驻留在名为大型机
的元素中:
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.
}
}
}
我只有空的catch块,因为如果插件上的操作失败,我会得到一个JavaScript错误。