最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Calling function on opener window - Stack Overflow

programmeradmin0浏览0评论

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.

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!

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.

Any ideas?

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.

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!

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.

Any ideas?

Share Improve this question edited Dec 23, 2011 at 1:26 user596075 asked Dec 23, 2011 at 1:05 TheCarverTheCarver 19.7k27 gold badges103 silver badges153 bronze badges 7
  • When is the closeWindow() function being called? When the user clicks a "close" button or something similar? And just to be totally clear, closeWindow() is in the child window, correct? – Drew Gaynor Commented Dec 23, 2011 at 1:31
  • possible duplicate of Popup Window.Opener Redirected or Closed – James Montagne Commented Dec 23, 2011 at 1:33
  • @JackieChiles yes closeWindow() is called when the users clicks a close button and the closeWindow() is in the child window. As I say, it works fine if the user doesn't reload the popup more than 7-8 times. – TheCarver Commented Dec 23, 2011 at 1:38
  • 1 @ThiefMaster - Yes, both Twitter and Facebook still use popup windows. Check Twitter developer pages. They can't be that bad :) – TheCarver Commented Jan 25, 2012 at 2:31
  • 1 and.. Google Fonts use popup windows. 3 big names still use them – TheCarver Commented Feb 1, 2012 at 20:37
 |  Show 2 more comments

1 Answer 1

Reset to default 17

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.

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(); 
}
发布评论

评论列表(0)

  1. 暂无评论