Javascript 开启窗口调用函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8611278/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calling function on opener window
提问by TheCarver
The child lost its parent!!
孩子失去了父母!!
I have a parent window, when somebody clicks on an image a JS popup opens and displays the photo and its information.
我有一个父窗口,当有人单击图像时,会打开一个 JS 弹出窗口并显示照片及其信息。
To close the popup/child window, and to flash an element on my parent/opener window, I have been using this function:
为了关闭弹出窗口/子窗口,并在我的父窗口/打开窗口上闪烁一个元素,我一直在使用这个函数:
function closeWindow() {
var currentID = document.getElementById('currentID').value;
window.opener.flashElement(currentID);
window.close();
}
My problem is this doesn't work if my users navigate away from the page that the popup originally opened. For example, in the popup window, there are next and previous buttons to scroll through the individual photos in that set of results, which reloads the page with a new querystring value.
我的问题是,如果我的用户离开弹出窗口最初打开的页面,这将不起作用。例如,在弹出窗口中,有下一个和上一个按钮可以滚动浏览该组结果中的单张照片,这会使用新的查询字符串值重新加载页面。
If my user scrolls (reloads page) less than 7 times it's okay but if they scroll more than that, the window.opener
function does not work, and because of that, the window.close
function doesn't either!
如果我的用户滚动(重新加载页面)少于 7 次没关系,但如果他们滚动超过 7 次,则该window.opener
功能不起作用,因此,该window.close
功能也不起作用!
I could probably rebuild the page so the data comes via an AJAX call, rather than reloading the page, but that's a lot of work I could do without.
我可能会重建页面,以便数据通过 AJAX 调用获得,而不是重新加载页面,但我可以做很多工作。
Any ideas?
有任何想法吗?
采纳答案by jermel
My guess is that
我的猜测是
window.opener.flashElement(currentID);
is throwing an error, or the function does not exist. Most likely the element with the value of currentID does not exist on the page. Try catching the error.
正在抛出错误,或者该函数不存在。页面上很可能不存在具有 currentID 值的元素。尝试捕获错误。
function closeWindow() {
var currentID = document.getElementById('currentID').value;
try {
window.opener.flashElement(currentID);
} catch (err) {
alert(err.description || err) //or console.log or however you debug
}
window.close();
}