Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling function on opener window

Tags:

javascript

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?

like image 231
TheCarver Avatar asked Dec 23 '11 01:12

TheCarver


1 Answers

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(); 
}
like image 69
jermel Avatar answered Sep 23 '22 05:09

jermel